Lists (aka recursive vectors) are the main data structure in R. Since lists are omnipresent (data.frames are a special sub-type) having a deeper understanding of them will make for a more enjoyable data analysis and helps avoid bugs.
Solutions are available here.
Exercise 1
Create a list called x
with two elements; two vectors of length 1 called a
and b
whose value is 1 and 2 respectively.
Exercise 2
Add a third vector of length 1 to x
, z = 3
.
Exercise 3
Turn your list into a named vector called y
with only one line of code.
Exercise 4
Create a new list z
which contains two vectors, y
and another one with 100 random standard normal numbers.
Exercise 5
With one line of code extract the median from each of the vectors in z
.
Exercise 6
Create a new list w
containing the dataset iris and z
, make sure each element is named. Now, using [[]]
on w
extract the Petal.width
vector. Note that the iris dataset is available in R by default, just type iris in the console.
Exercise 7
Remove both a
and c
from the list x
from question 2.
Exercise 8
Create a new list using the following code:
dfs <- list( df1 = data.frame(s="a", t=1:5), df2 = data.frame(s="b", t=111:115), df3 = data.frame(s="c", t=300:310) ) Usingdo.call()
bind them toegher in one masterdata.frame
.
Exercise 9
Select df2
from the list (dfs
) using the following syntax "[["(a, b)
with the right arguments a
and b
.
Exercise 10
Using a similar call as above, make a new data.frame
consisting of rows 3:4 from each of the data.frames in dfs
.
(Picture by Shaofu Ku)
Leave a Reply