Applying Functions to Data Frames
As with lists, you can use the
sapply functions with data frames.
Using lapply() and sapply() on 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() 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  "Jack" "Jill" $ages  10 12
dl is a list consisting of two vectors, the sorted versions of
dl is just a list, not a data frame. We could ...