2;2;2;2;2;2;2;2;2;2;2;8;8;8;8;1;2;3;4;5;6;7;8;9;10;6;5;4;3;2;1;56;56;56;56;8;8;8;8;8
From the above number series construct a vector x using
the functions c(), rep() and
seq(). Note, you must use all three functions! Read the
online script from the PCLab to answer the questions.
x <- c(
rep(2, times = 11),
rep(8, times = 4),
seq(1:10),
seq(6:1),
rep(56, times = 4),
rep(8, times = 5)
)
x
## [1] 2 2 2 2 2 2 2 2 2 2 2 8 8 8 8 1 2 3 4 5 6 7 8 9 10
## [26] 1 2 3 4 5 6 56 56 56 56 8 8 8 8 8
x?length(x)
## [1] 40
x.x[12]
## [1] 8
x.x[20:7]
## [1] 5 4 3 2 1 8 8 8 8 2 2 2 2 2
x.x[c(12, 20:27)]
## [1] 8 5 6 7 8 9 10 1 2
x and
assign the new vector to a variable y. Print the content of
y.x[-5]->y
y
## [1] 2 2 2 2 2 2 2 2 2 2 8 8 8 8 1 2 3 4 5 6 7 8 9 10 1
## [26] 2 3 4 5 6 56 56 56 56 8 8 8 8 8
i in vector x whether i is equal
to 56. The result should be a logical vector of
TRUEs and FALSEs that has the same number of
elements as x.x==56
## [1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [13] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [25] FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE TRUE TRUE TRUE FALSE
## [37] FALSE FALSE FALSE FALSE
56
in vector x with the value 52. Print the
content of x.x[x==56]<- 52
x
## [1] 2 2 2 2 2 2 2 2 2 2 2 8 8 8 8 1 2 3 4 5 6 7 8 9 10
## [26] 1 2 3 4 5 6 52 52 52 52 8 8 8 8 8
x[x < 5 | x > 50] <- NA
x
## [1] NA NA NA NA NA NA NA NA NA NA NA 8 8 8 8 NA NA NA NA 5 6 7 8 9 10
## [26] NA NA NA NA 5 6 NA NA NA NA 8 8 8 8 8
5 to each element in vector
x.x=x+5
x
## [1] NA NA NA NA NA NA NA NA NA NA NA 13 13 13 13 NA NA NA NA 10 11 12 13 14 15
## [26] NA NA NA NA 10 11 NA NA NA NA 13 13 13 13 13