Please find below the solutions for the first 5 exercises of our set Creating vectors. The solutions for exercise 6, 7 and 8 are available in our eBook Start Here To Learn R – vol. 1: Vectors, arithmetic, and regular sequences.
Solution 3
c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
## [1] 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3
## [24] 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6
## [47] 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9
## [70] 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2
## [93] 3 4 5 6 7 8 9 10
The number in square brackets is a convenience counter that R adds to its output, which helps to locate the position in the vector of each element shown.
Solution 4
Part a
This works, but is not preferred for stylistic reasons:
a<-c(1, 2, 3, 4, 5)
a
## [1] 1 2 3 4 5
Part b
Adding a space before and after the <-
operator, as in (b), leads to cleaner code that is easier to read.
a <- c(50, 60, 70, 80, 90)
a
## [1] 50 60 70 80 90
Part c
Although it is possible to reverse the <-
operator, it should always point from the vector to its name, not from the name to the vector. So this gives an error:
a -> c(20, 31, 42, 53, 64)
## Error in c(20, 31, 42, 53, 64) <- a : target of assignment
## expands to non-language object
Because the previous statement led to an error, a
didn’t change. So, if we look up the contents of a
, we will see the same output as for part (b).
a
## [1] 50 60 70 80 90
Part d
R throws an error again, because the <-
operator points from the name to the vector, instead of from the name to the vector:
c(5, 6, 7, 9, 10) <- a
## Error in c(5, 6, 7, 9, 10) <- a : target of assignment
## expands to non-language object
And, like before in part (c), a
didn’t change. So, if we look up the contents of a
, we will still see the same output as for part (b).
a
## [1] 50 60 70 80 90
Part e
The following statement works, because the <-
operator points from the vector to the name.
c(101, 102, 103, 104, 105) -> a
a
## [1] 101 102 103 104 105
Part f
The <
and -
part of the <-
operator always have to be adjacent. So in the following statement, R reads the <
operator, and then a -
sign, instead of the <-
operator. It executes the statement as a comparison (a topic we’ll come to later), and doesn’t throw an error. Note that a
didn’t change.
a < - c(11, 12, 13, 14, 15)
## [1] FALSE FALSE FALSE FALSE FALSE
a
## [1] 101 102 103 104 105
Part g
Again, the <
and -
part of the <-
operator always have to be adjacent, so the following is identical to part (f).
a < -c(100, 99, 88, 77, 66)
## [1] FALSE FALSE FALSE FALSE FALSE
a
## [1] 101 102 103 104 105
Part h
Finally, instead of <-
, we can also use the assign()
function. But beware that the name of the vector has to appear between quotes. So, the following gives an error:
assign(a, c(1000, 2000, 3000, 4000, 5000))
## Error in assign(a, c(1000, 2000, 3000, 4000, 5000)) :
## invalid first argument
a
## [1] 101 102 103 104 105
Part i
Below we use the assign
function correctly.
assign('a', c(83, 16, 35, 58, 3))
a
## [1] 83 16 35 58 3
Solution 5
evenNumbers <- c(2, 4, 6, 8, 10, 12, 14, 16, 20)
zero <- 0
pi <- 3.141593
powersOfTen <- c(1, 10, 100, 1000, 10000, 100000)
Solution 6
The solution for this exercise is available in our eBook Start Here To Learn R – vol. 1: Vectors, arithmetic, and regular sequences.
Solution 7
The solution for this exercise is available in our eBook Start Here To Learn R – vol. 1: Vectors, arithmetic, and regular sequences.
Solution 8
The solution for this exercise is available in our eBook Start Here To Learn R – vol. 1: Vectors, arithmetic, and regular sequences.
Leave a Reply