[R] apply function with grouped columns

Jack Siegrist jacksie at eden.rutgers.edu
Fri Jan 29 03:45:02 CET 2010


I have a data set of many rows and many columns in which both the rows and
the columns have associated grouping factors.

Is there a way to do what 'aggregate' does but in the other dimension?

The way I have been doing this is to use 'aggregate' on the data in the
usual way and then rotate the result and apply 'aggregate' again. This
works but is a little messy and I was wondering if there is some built-in
way to do this easier.

An example of what I have been doing is below.

#Four observations of four variables
v1 <- c(1, 8, 5, 3)
v2 <- c(5, 5, 6, 5)
v3 <- c(3, 2, 9, 4)
v4 <- c(4, 1, 1, 1)

myData <- data.frame(v1=v1, v2=v2, v3=v3, v4=v4)
myData

#The observations have either property 1 or property 2
rowFactor <- data.frame(RowTraits=factor(c(1, 2, 1, 2)))
rowFactor

#The variables have a property that is either present or absent
colFactor <- data.frame(ColTraits=factor(c(1, 1, 0, 0)))
colFactor

#Getting the means for the columns by row groups is easy
MeanByRowTraits <- aggregate (myData, rowFactor[1], mean)
MeanByRowTraits

#But now to get the means for the rows by column groups is awkward
rotateData <- data.frame(t(MeanByRowTraits[2:5]))
colnames(rotateData) <- c(levels(rowFactor[,1]))
rotateData

#This is the kind of result I want in the end
aggregate (rotateData, colFactor[1], mean)

-- 
Jack Siegrist
Graduate Program in Ecology & Evolution,
and Department of Ecology, Evolution, & Natural Resources,
Rutgers University, New Brunswick, NJ 08901



More information about the R-help mailing list