## Exercise 1. Assignment statement
Take a look at the code below. What would be the value of `x` at the end? Write down your answer **before** running code chunks!
**Answer:**
```{r exercise 01-1}
x <- 5
x <- 3
print(x)
```
**Answer:**
```{r exercise 01-2}
x <- 5
x <- x * x
print(x)
```
**Answer:**
```{r exercise 01-3}
x <- 5
x <- x / x
x <- 2
print(x)
```
## Exercise 2. Assigning values (vectors) to variables
Take a look at the code below. What would be the results of computation in each chunk? Write down your answer **before** running code chunks!
**Answer:**
```{r exercise 02-1}
x <- c(1, 3, 5)
y <- x
x <- c(3, 4, 1)
z <- y
z
```
**Answer:**
```{r exercise 02-2}
x <- c(1, 3, 5)
y <- x
x <- c(3, 4, 1)
z <- x
z
```
**Answer:**
```{r exercise 02-3}
x <- c(1, 3, 5)
y <- x
x <- c(3, 4, 1)
z <- x
z <- 2
z
```
## Exercise 3. Concatenating vectors.
Take a look at the code below. What would be the results of the computation? Write down your answer **before** running the code chunk!
**Answer:**
```{r exercise 03}
x <- c(1, 3, 5)
y <- c(2, 4, 2)
z <- c(3, 4, 1)
c(y, 6, z, x, c(10, 11), x)
```
## Exercise 4. Vector indexing.
What will be an output of each chunk? Write down your answers and then run chunks to check.
**Answer #1:**
```{r exercise 04-01}
x <- c(2, 13, 4, 25, 4, 122, -12, 78)
# 1
print(x[2])
```
**Answer #2:**
```{r exercise 04-02}
#2
x <- c(2, 13, 4, 25, 4, 122, -12, 78)
x[0] <- 5
print(x)
```
**Answer #3:**
```{r exercise 04-03}
#3
x <- c(2, 13, 4, 25, 4, 122, -12, 78)
print(x[10])
```
**Answer #4:**
```{r exercise 04-04}
#4
x <- c(2, 13, 4, 25, 4, 122, -12, 78)
x[8] <- 8
print(x)
```
**Answer #5:**
```{r exercise 04-05}
#4
x <- c(2, 13, 4, 25, 4, 122, -12, 78)
x[12] <- 12
x[0] <- 13
print(x)
```
## Exercise 5. Zero and out-of-range indexing
What will be an output of each chunk? Write down your answers and then run chunks to check.
**Answer #1:**
```{r exercise 05-01}
x <- c(2, 13, 4, 25, 4, 122, -12, 78)
print(x[0])
```
**Answer #2:**
```{r exercise 05-02}
x <- c(2, 13, 4, 25, 4, 122, -12, 78)
print(x[10])
```
**Answer #3:**
```{r exercise 05-03}
x <- c(2, 13, 4, 25, 4, 122, -12, 78)
print(x[10])
```
**Answer #4:**
```{r exercise 05-04}
x <- c(2, 13, 4, 25, 4, 122, -12, 78)
x[10] <- 100
print(x[9])
```
**Answer #5:**
```{r exercise 05-05}
x <- c(13, 4, 25, 4, 122, -12, 78, 2)
x[10] <- 100
print(x[8])
```
**Answer #6:**
```{r exercise 05-06}
x <- c(13, 4, 25, 4, 122, -12, 78, 2)
x[0] <- 100
print(x)
```
## Exercise #6
What will be an output of each chunk? Write down your answers and then run chunks to check.
**Answer #1:**
```{r exercise 06-01}
x <- c(1, 2, 3, 4, 5)
# 1
print(x[-4])
```
**Answer #2:**
```{r exercise 06-02}
x <- c(1, 2, 3, 4, 5)
# 1
print(x[-10])
```
**Answer #3:**
```{r exercise 06-03}
x <- c(1, 2, 3, 4, 5)
x[-2] <- 10
x[-3] <- 8
# 1
print(x)
```
## Exercise #7. Named vectors
What will be an output of each chunk? Write down your answers and then run chunks to check.
**Answer #1:**
```{r exercise 07-01}
point <- c("x"=20, "y"=10, "z"=1)
point[3] <- point["x"] + point[2]
print(point["z"])
```
**Answer #2:**
```{r exercise 07-02}
point <- c("x"=20, "y"=10, "z"=1)
names(point)[3] <- "Zzzzz..."
print(names(point))
```
**Answer #3:**
```{r exercise 07-03}
point <- c("x"=20, "y"=10, "z"=1)
names(point) <- c("c", "b", "a")
print(point["a"])
```
## Exercise #8. Colon operator and slicing.
What will be an output of each chunk? Write down your answers and then run chunks to check. Hint, first figure out and write down vector values. Next, do the same for the index vector. Then, see what values you get will get from that index.
**Answer #1:**
```{r exercise 08-01}
x <- 10:18
print(x[4])
```
**Answer #2:**
```{r exercise 08-02}
x <- 20:10
print(x[3:2])
```
**Answer #3:**
```{r exercise 08-03}
x <- 10:20
print(x[-4:-1])
```
**Answer #4:**
```{r exercise 08-04}
x <- 10:20
x[seq(0, 12, by=2)]
```
## Exercise 9. Vector operations.
Take a look at the code below. What would be the results of the computation? Write down your answer **before** running the code chunk!
**Answer:**
```{r exercise 09}
x <- c(1, 3, 5)
y <- c(2, 4, 2)
z <- c(3, 4, 1)
x * y + z
```
## Exercise 10. Vector operations, mismatching lengths
Take a look at the code below. What would be the results of the computation? Write down your answer **before** running the code chunk!
**Answer #1:**
```{r exercise 10-01}
x <- c(1, 3, 5, 2)
y <- c(2, 4)
z <- c(3, 4, 1, 4, 7, 2, 6, 2)
x * y + z
```
**Answer #2:**
```{r exercise 10-02}
x <- c(1, 3, 5, 2)
y <- c() # Hint: you concatenate values into a vector but there are zero values listed. What kind of vector you will get?
z <- c(3, 4, 1, 4, 7, 2, 6, 2)
x * y + z
```
## Exercise 11. Let's see if R can suprise you!
This exercise has dual purpose. First, it reminds you that, as far as simple data types, everything is a vector. A number you type is a vector. Index you use to subset a vector is a vector. Values or a value you get from a vector is a vector. Even if it is a single value you are using / accessing, it is a single-element vector.
Second, keep in mind that computers are dumb and interpret the code literally. Therefore, it will be easy for you figure out what the code does, if you pretend to be a computer and "execute" the code piece-by-piece. In case of brackets, the rules are left-to-right and inside-out. If you have several brackets one after another(e.g., `x[.#1.][.#2.][.#3.]`), you start with the first one (`x[.#1.]`) and figure out what _vector_ will you get. Then, you figure out what happens when you apply `[.#2.]` to that _new_ vector, etc. In case of nested brackets `x[y[z[w]]]`, you start with the innermost (`z[w]`) figure out what the _vector_ will be, substitute into the next level `y[vector-from-inner-brackets]` and figure out that second _vector_. Put it into the next level, etc.
Take a look at the code below. What would be the results of the computation? Write down your answer **before** running the code chunk!
**Answer #1:**
```{r exercise 11-01}
3[1]
```
**Answer #2:**
```{r exercise 11-02}
3[1][1]
```
**Answer: #3**
```{r exercise 11-03}
x <- 1:10
x[5][1][2]
```
**Answer: #4**
```{r exercise 11-04}
x <- 1:10
x[1:5][1[1]]
```
**Answer: #5**
```{r exercise 11-05}
x <- 1:10
x[1:5][2[1]]
```
**Answer: #6**
```{r exercise 11-06}
x <- 1:10
x[0][-2]
```
**Answer: #7**
```{r exercise 11-07}
5[1:5][-2]
```
**Answer: #8**
```{r exercise 11-08}
5[-1]
```
## Exercise 12
Three very hungry participants attempt to break world record for most hot dogs eaten in 10 minutes (currently, 74). After 8 minutes they eaten 45, 69, and 57 hot dogs. How many hot dogs per minute must each of the eat to break the record? Store three values in a _single_ vector and compute the number of hot dogs per minute in a single line for all three participants at once.
```{r exercise 12}
# your code goes here
```
## Exercise 13
Space X made following number of launches in each year from 2010 till 2017: 2, 0, 2, 3, 6, 7, 9, 18. Of which failures were: 0, 0, 0, 0, 0, 1, 1, 0. Compute number of **successful** launches for each year. Both sequences should be stored in variables (come up with appropriate names, no `x` or `y`) and a computation should be a single formula applied computing it for all years.
```{r exercise 13}
# your code goes here
```
## Exercise 14
Using information from exercise 13 (you don't need to redefine variables), compute the total number of launches. Use `sum()` function (type "?sum()" in console for documentation).
```{r exercise 14}
# your code goes here
```
## Exercise 15
Using information from exercise 13, compute the total number of _successful_ launches.
```{r exercise 15}
# your code goes here
```
## Exercise 16
Using information from exercise 13, compute success rate _per year_.
```{r exercise 16}
# your code goes here
```
## Exercise 17
Using information from exercise 13, compute average success over years. For this compute success rate per year and then compute average via `mean()` function (type "?mean()" in console for documentation)
```{r exercise 17}
# your code goes here
```
## Exercise 18
Create a vector of first 10 positive odd integers. Compute median of these values ("?median()"). Your choice on whether you use a temporary variable (if you do, name it properly, not "x" or "y")
```{r exercise 18}
# your code goes here
```
## Exercise 19
Create a vector of first 5 positive odd integers (ascending order) and another vector of 5 negative event integers (also ascending order), compute pairwise sum of values.
```{r exercise 19}
# your code goes here
```