v <- c(2, 4, 5, 6, 4, -1)Exercise 1: Solutions second part
Task 2.1
Assign a new object v that is a vector with the elements c(2, 4, 5, 6, 4, -1).
Task 2.2
Add 1 to each element of the vector?
v_plus_1 <- v + 1 # automatic recycling to full length
v_plus_1[1] 3 5 6 7 5 0
Task 2.3
Swap the sign of the vector
v_swapped_sign <- -v
v_swapped_sign[1] -2 -4 -5 -6 -4 1
Task 2.4
Get the second element of the vector
second_element <- v[2]
second_element[1] 4
Task 2.5
Get all elements of the vector except the last one.
v_except_last1 <- v[-length(v)]
v_except_last1[1] 2 4 5 6 4
v_except_last2 <- v[1:(length(v)-1)]
v_except_last2[1] 2 4 5 6 4
Task 2.6
How many values of 4 does the vector contain?
count_4s <- sum(v == 4)
count_4s[1] 2
Task 2.7
Swap the sign of all negative values.
v[v < 0] <- -v[v < 0]
v[1] 2 4 5 6 4 1