[R] How do I tapply to a data frame with arbitrary column labels?
Bill McNeill (UW)
billmcn at u.washington.edu
Fri Dec 12 00:25:16 CET 2008
I have a data file that looks like this
class pigeon falcon propeller jet wing fly
birds 25 37 0 0 2 1
planes 0 1 28 40 1 3
birds 19 41 0 1 4 6
planes 0 0 25 50 5 5
planes 1 0 30 45 3 5
There may be an arbitrary number of unique class labels and an
arbitrary number of uniquely named columns. I use read.table to read
my file into a data frame. I then want to take the sum of all the
column values for each of the class types. That is I want to end up
with:
class pigeon falcon propeller jet wing fly
birds 44 78 0 1 6 7
planes 1 1 83 135 9 13
where (pigeon, birds) = 25 + 19 = 44, (propeller, planes) = 28 + 25 +
30 = 83, etc.
I see how to use tapply to get the sum for each individual column, e.g.
> tapply(data$falcon, data$class, sum)
birds planes
78 1
But I can't figure out what command will do these sums for the entire
data frame without me having to specify the column names in the
command.
Thanks.
--
Bill McNeill
http://staff.washington.edu/billmcn/index.shtml
More information about the R-help
mailing list