Bill McNeill (UW)
2008-Dec-11 23:25 UTC
[R] How do I tapply to a data frame with arbitrary column labels?
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
Gabor Grothendieck
2008-Dec-12 01:19 UTC
[R] How do I tapply to a data frame with arbitrary column labels?
Look at rowsum or aggregate:> Lines <- "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"> > DF <- read.table(textConnection(Lines), header = TRUE)> rowsum(DF[-1], DF[,1])pigeon falcon propeller jet wing fly birds 44 78 0 1 6 7 planes 1 1 83 135 9 13> aggregate(DF[-1], DF[1], sum)class pigeon falcon propeller jet wing fly 1 birds 44 78 0 1 6 7 2 planes 1 1 83 135 9 13 On Thu, Dec 11, 2008 at 6:25 PM, Bill McNeill (UW) <billmcn at u.washington.edu> wrote:> 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 > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >
Reasonably Related Threads
- How do I reload sessions from a non-default directory in OS X?
- How do I multiply labeled vectors of numbers?
- How do I generate one vector for every row of a data frame?
- [RFC] Propeller: A frame work for Post Link Optimizations
- [RFC] Propeller: A frame work for Post Link Optimizations