Below are the solutions to these exercises on list vol. 2.

#################### # # # Exercise 1 # # # #################### #Consider 3 vectors, day, month and year: year=c(2005:2016) month=c(1:12) day=c(1:31) #Define a list Date such as: #Date= #$year #[1] 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 #$month # [1] 1 2 3 4 5 6 7 8 9 10 11 12 #$day # [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 Date=list() Date$year=year Date$month=month Date$day=day Date

## $year ## [1] 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 ## ## $month ## [1] 1 2 3 4 5 6 7 8 9 10 11 12 ## ## $day ## [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 ## [24] 24 25 26 27 28 29 30 31

#################### # # # Exercise 2 # # # #################### #write an R statement that will replace the values of year element in Date list for c(2000:2010) Date$year=c(2000:2010) Date

## $year ## [1] 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 ## ## $month ## [1] 1 2 3 4 5 6 7 8 9 10 11 12 ## ## $day ## [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 ## [24] 24 25 26 27 28 29 30 31

#################### # # # Exercise 3 # # # #################### #write an R statement that will delete the value 4 of the month component of the list Date. Date$month=Date$month[-4] Date

## $year ## [1] 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 ## ## $month ## [1] 1 2 3 5 6 7 8 9 10 11 12 ## ## $day ## [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 ## [24] 24 25 26 27 28 29 30 31

#################### # # # Exercise 4 # # # #################### #Consider a vector x such that: x=c(1,3,4,7,11,18,29) #Write an R statement that will return a list with components of value: #x*2,x/2,sqrt(x) and names "x*2","x/2","sqrt(x)" X2=list('x*2'=x*2,'x/2'=x/2,'sqrt(x)'=sqrt(x)) X2

## $`x*2` ## [1] 2 6 8 14 22 36 58 ## ## $`x/2` ## [1] 0.5 1.5 2.0 3.5 5.5 9.0 14.5 ## ## $`sqrt(x)` ## [1] 1.000000 1.732051 2.000000 2.645751 3.316625 4.242641 5.385165

#################### # # # Exercise 5 # # # #################### #Consider the X2 list. #Write an R statement that will return a vector: #2.000000 2.645751 3.316625 X2[[3]][3:5]

## [1] 2.000000 2.645751 3.316625

#################### # # # Exercise 6 # # # #################### #Write an R statement that will return a concatenation of Date and X2 lists. M=c(Date,X2) M

## $year ## [1] 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 ## ## $month ## [1] 1 2 3 5 6 7 8 9 10 11 12 ## ## $day ## [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 ## [24] 24 25 26 27 28 29 30 31 ## ## $`x*2` ## [1] 2 6 8 14 22 36 58 ## ## $`x/2` ## [1] 0.5 1.5 2.0 3.5 5.5 9.0 14.5 ## ## $`sqrt(x)` ## [1] 1.000000 1.732051 2.000000 2.645751 3.316625 4.242641 5.385165

#################### # # # Exercise 7 # # # #################### #Write an R statement that will return a sublist N of M, with components year,x*2 and day. N=M[c('year','x*2','day')] N

## $year ## [1] 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 ## ## $`x*2` ## [1] 2 6 8 14 22 36 58 ## ## $day ## [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 ## [24] 24 25 26 27 28 29 30 31

#################### # # # Exercise 8 # # # #################### #Consider the N list. #Write an R statement that will return: #-the length of x*2 vector in N #-the value of the second element of vector year in N length(N[[2]])

## [1] 7

N$year[2]

## [1] 2001

#################### # # # Exercise 9 # # # #################### #Consider 3 letters vectors, and 2 numeric vectors: A=letters[1:4];B=letters[5:10];C=letters[11:15] D=c(1:10);E=c(20:5) #Define a list z, with elementes x and y,such that #x is a list with elements A,B,and C. #and y is alist with elements D and E. x=list(A,B,C) y=list(D,E) z=list(x,y) z

## $X ## $X$A ## [1] 1 2 3 4 5 6 7 8 9 10 ## ## $X$B ## [1] 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 ## ## ## $Y ## $Y$A ## [1] "a" "b" "c" "d" ## ## $Y$B ## [1] "e" "f" "g" "h" "i" "j" ## ## $Y$C ## [1] "k" "l" "m" "n" "o"

#################### # # # Exercise 10 # # # #################### #Write an R statement that will return: #-the number on third position on the second vector of the first list of z #-the letter on fifth position on the third vector of the second list of z z[[1]][[2]][3]

## [1] 18

z[[2]][[3]][5]

## [1] "o"

**What's next:**

- Become a Top R Programmer Fast with our Individual Coaching Program
- Explore all our (>4000) R exercises
- Find an R course using our R Course Finder directory
- Subscribe to receive weekly updates and bonus sets by email
- Share with your friends and colleagues using the buttons below

anon says

Number 9 answer is wrong I think.

Carlos says

I agree. In that exercise the answer is correct, but the output is not. Also, in exercise 10, for a), the output must be “g”, instead of 18.

Carlos says

Thanks a lot for the exercises. They are very helpful.