October 2011
Beginner to intermediate
400 pages
9h 30m
English
As with lists, you can use the lapply and sapply functions with data frames.
Keep in mind that data frames are special cases of lists, with the list components consisting of the data frame’s columns. Thus, if you call lapply() on a data frame with a specified function f(), then f() will be called on each of the frame’s columns, with the return values placed in a list.
For instance, with our previous example, we can use lapply as follows:
> d kids ages 1 Jack 12 2 Jill 10 > dl <- lapply(d,sort) > dl $kids [1] "Jack" "Jill" $ages [1] 10 12
So, dl is a list consisting of two vectors, the sorted versions of kids and ages.
Note that dl is just a list, not a data frame. We could ...
Read now
Unlock full access