(For this exercise, first write down your answer, without using R. Then, check your answer using R.
1.2.2.1 if x <- c(a = 1, b = 2,c=3,d=4) What is the output for the code: seq(5,11,along.with =x)
## The length of vector x is 4. So, the code will give us a sequence which is from 5 to 11 and the length is 4 which suppose to be 5,7,9,11.
x <- c(a=1,b=2,c=3,d=4)
seq(5,11,along.with = x)
## [1] 5 7 9 11
1.2.2.2 If x= seq(4,12,4) ,what is the output for the code: rep(x,each=2)
## x is a sequence from 4 to 12, increases by 4. So it is 4, 8, 12. We are replicating each value for 2 times so the code will give this one: 4 4 8 8 12 12.
x <- seq(4,12,4)
rep(x,each=2)
## [1] 4 4 8 8 12 12
1.2.2.3 What is the output for the code: seq(5,11,by=2,length.out=3)
## should give an error. Because if we increase 5 by 2, the sequence will be 5,7,9,11. Last argument contradicts with the "by" argument.
1.2.2.4 What is the output for the code: rep(letters[1:10],3)
## creates a sequence with first ten letters for 3 times. a..j a...j a...j
rep(letters[1:10],3)
## [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "a" "b" "c" "d" "e" "f" "g"
## [18] "h" "i" "j" "a" "b" "c" "d" "e" "f" "g" "h" "i" "j"
1.2.2.5 Create a sequence with values: 100 95 90 85 80 75 70 65 60 55 50
##decrease 100 by 5 to 50..easy ha?
seq(100,50,-5)
## [1] 100 95 90 85 80 75 70 65 60 55 50
1.2.2.6 What is the output for the code: seq(10,0,by=5)
## Error. Don't forget the minus while decreasing! This one works:
seq(10,0,by=-5)
## [1] 10 5 0
1.2.2.7 What is the output for the code: seq(2,10,by=4)==c(2,6,10)
## It will give a logical output. Sequence will be 2,6,10 which means each value of the vector will match with the values in the sequence so the output will be TRUE statement for each. TRUE TRUE FALSE for the one below, for example.
seq(2,10,by=4)==c(2,6,12)
## [1] TRUE TRUE FALSE
1.2.2.8 What is the output for the code: rep(c(‘seq’,‘rep’),each=4)
## replicates each value for four times. seq seq...rep rep...
rep(c('seq','rep'),each=4)
## [1] "seq" "seq" "seq" "seq" "rep" "rep" "rep" "rep"
1.2.2.9 Consider two variables A= as.Date(“2016-11-01”) and B = as.Date(“2016-11-15”). What is the output for the code: seq.Date(A,B, by = “1 day”)
## You can also sequence dates! The logic is the same. sequence will be the days from 1st of November to 15th of November.
A= as.Date("2016-11-01")
B = as.Date("2016-11-15")
seq.Date(A,B, by = "1 day")
## [1] "2016-11-01" "2016-11-02" "2016-11-03" "2016-11-04" "2016-11-05"
## [6] "2016-11-06" "2016-11-07" "2016-11-08" "2016-11-09" "2016-11-10"
## [11] "2016-11-11" "2016-11-12" "2016-11-13" "2016-11-14" "2016-11-15"
1.2.2.10 Consider two variables C= as.Date(“2016-02-01”) and D = as.Date(“2016-06-15”). What is the output for the code: seq.Date(D,C, by = “-1 month”)
## Same logic with decreasing the values. Output will be a sequence of dates from June to Feb. like 15.06, 15.05, 15.04....
C= as.Date("2016-02-01")
D = as.Date("2016-06-15")
seq.Date(D,C, by = "-1 month")
## [1] "2016-06-15" "2016-05-15" "2016-04-15" "2016-03-15" "2016-02-15"