Answers to the exercises are available here.

**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

**Exercise 2**

write an R statement that will replace the values of year element in Date list for `c(2000:2010)`

.

**Exercise 3**

write an R statement that will delete the value 4 of the month component of the list Date.

**Learn more**about lists in the online courses:

- Learn By Example: Statistics and Data Science in R (lecture 59)
- The Comprehensive Statistics and Data Science with R Course (lectures 69-76)

- R Programming: Advanced Analytics In R For Data Science (11 lectures all about lists, ~ 1.5 hrs in total)

**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 X2 with components of value:

`x*2,x/2,sqrt(x)`

and names `"x*2","x/2","sqrt(x)"`

**Exercise 5**

Consider the X2 list.

Write an R statement that will return a vector:

`2.000000 2.645751 3.316625`

**Exercise 6**

Write an R statement that will return a concatenation M, of Date and X2 lists.

M

$year

[1] 2000 2001 2002 2003 2004 2005 2006 2007 2008

$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.

**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

**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.

**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

**Want some extra practice with lists? Please take a look here**

## Leave a Reply