Use five different ways to create an equally-spaced sequence with 2 4 6 8 10 as result.
x1 <- c(2,4,6,8,10)
x1
## [1] 2 4 6 8 10
x2 <- seq(from = 2, to = 10, by = 2)
x2
## [1] 2 4 6 8 10
x3 <- seq(from =2 , to =10, length.out = 5)
x3
## [1] 2 4 6 8 10
x4 <-seq(from = 2, by = 2, length.out=5)
x4
## [1] 2 4 6 8 10
x5 <-rep(2:10,c(1,0,1,0,1,0,1,0,1))
x5
## [1] 2 4 6 8 10
Use two different ways to create a numeric vector with 1 2 3 1 2 3 4 5 1 2 3 4 5 6 7 as result. Show the unique elements and their corresponding frequency.
x6 <- sequence(c(3,5,7))
x6
## [1] 1 2 3 1 2 3 4 5 1 2 3 4 5 6 7
x7 <- c(seq(from = 1, to = 3, by = 1),
seq(from = 1, to = 5, by = 1),
seq(from = 1, to = 7, by = 1))
x7
## [1] 1 2 3 1 2 3 4 5 1 2 3 4 5 6 7
Write R code using rep() function to create a character vector with the same result as c(“sheep”,“pig”, “cat”,“sheep”,“pig”, “cat”,“sheep”,“pig”, “cat”)
animals1 <- rep(c("sheep","pig", "cat"),3)
animals1
## [1] "sheep" "pig" "cat" "sheep" "pig" "cat" "sheep" "pig" "cat"
Use rep() function to create character vector with the same result as c(“sheep”,“sheep”,“pig”,“pig”,“pig”,“pig”,“cat”,“cat”,“cat”)
animals2 <- rep(c("sheep","pig", "cat"),c(2,4,3))
animals2
## [1] "sheep" "sheep" "pig" "pig" "pig" "pig" "cat" "cat" "cat"
Create a numeric vector named exe with values 2, 0, -3, 0, 5, 6 and sort exe from the largest to the smallest.
exe <- c(2, 0, -3, 0, 5, 6)
exe
## [1] 2 0 -3 0 5 6
sort(exe,decreasing = TRUE)
## [1] 6 5 2 0 0 -3
In exe, what’s the ranks of 2 and the first 0?
rank(exe)
## [1] 4.0 2.5 1.0 2.5 5.0 6.0
#the rank of 2 is 4.0 and the rank of fisrt0 is 2.5
For exe, get indices for the elements in the ascending order.
sort(exe,decreasing=FALSE)
## [1] -3 0 0 2 5 6
Create a character vector with values “&5”, “Nd”, “9iC”, “3df”, “df”, “nd”, "_5“,”9ic" and sort it in the ascending order. Then, explain 1) why 3df goes before 9ic; 2) why &5 goes before 3df; 3) why 9ic goes before 9iC.
Char1 <- c("&5", "Nd", "9iC", "3df", "df", "nd", "_5", "9ic")
Char1
## [1] "&5" "Nd" "9iC" "3df" "df" "nd" "_5" "9ic"
sort(Char1,decreasing=FALSE)
## [1] "_5" "&5" "3df" "9ic" "9iC" "df" "nd" "Nd"
#1) why 3df goes before 9ic:Cause 3 is smaller than 9,digits are in an ascending order,the smaller digits appear earlier than the bigger ones ,so "3df" goes before "9ic";
#2) why &5 goes before 3df :Cause the ordering rules of a single character tells us that "symbols appear first,followes by digits,and letters come last"; "&5" starts with a symbol,and "3df" starts with a number,so "&5" goes before "3df";
#3) why 9ic goes before 9iC: the first two letters of "9ic"and"9iC" are same, so we compare the third letter."9ic" has a lower c while"9iC" has an upper C, the lower alphabet comes first.
Suppose x <- c(5, 2, 4, 1, 2, 1), y <- c(T, F, F, F, F, T, T, F, F, T) Write R code to reproduce each element of the summary vector summary(x)
x <- c(5, 2, 4, 1, 2, 1)
summary(x)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 1.00 1.25 2.00 2.50 3.50 5.00
Write R code to generate the cumulative sum, cumulative product, cumulative minimum, and cumulative maximum of x.
x <- c(5, 2, 4, 1, 2, 1)
cumsum(x)
## [1] 5 7 11 12 14 15
cumprod(x)
## [1] 5 10 40 40 80 80
cummin(x)
## [1] 5 2 2 1 1 1
cummax(x)
## [1] 5 5 5 5 5 5
Write R code to generate a vector consisting of the 0.1, 0.2, 0.6, 0.8, 0.9 quantiles of x.
quantilex <- quantile(x, probs = c(0.1, 0.2, 0.6, 0.8, 0.9))
quantilex
## 10% 20% 60% 80% 90%
## 1.0 1.0 2.0 4.0 4.5
Write R code to calculate the sample variance and sample standard deviation of x.
var(x)
## [1] 2.7
sd(x)
## [1] 1.643168
Write R code to generate a length-2 vector consisting of the sum and mean of y. Then show the unique elements and their corresponding frequency.
y <- c(TRUE, FALSE, FALSE, FALSE, FALSE, TRUE, TRUE, FALSE, FALSE, TRUE)
sum(y)
## [1] 4
mean(y)
## [1] 0.4
y_vec <- c(sum(y),mean(y))
y_vec
## [1] 4.0 0.4
Consider the vector v1 <- c(7, 2, 4, 9, 7), v2 <- c(6, 2, 8, 7, 9), and v3 <- 1:50. Find the indices in v1 where the corresponding value is smaller than v2.
v1 <- c(7, 2, 4, 9, 7)
v2 <- c(6, 2, 8, 7, 9)
v1[v1 < v2]
## [1] 4 7
Find the subvector of v2 such that the corresponding location in v1 is larger than 5.
subv2 <- v2[v1>5]
subv2
## [1] 6 7 9
Find the subvector of v3 such that it is divisible by 7. (Hint: the result of 7%%7 is equal to 0 since 7 is divisible by 7)
v3 <- 1:50
subv3 <- v3[v3%%7 ==0]
subv3
## [1] 7 14 21 28 35 42 49
For all elements of v3 that is divisible by 8, replace it by 100.
sub_v3 <- v3[v3%%8 ==0]
v3[sub_v3] <-100
v3
## [1] 1 2 3 4 5 6 7 100 9 10 11 12 13 14 15 100 17 18 19
## [20] 20 21 22 23 100 25 26 27 28 29 30 31 100 33 34 35 36 37 38
## [39] 39 100 41 42 43 44 45 46 47 100 49 50