Skip to content

apply function with grouped columns

2 messages · Jack Siegrist, Peter Ehlers

#
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)
#
Try this:

y <- c(v1, v2, v3, v4)
rowf <- gl(2, 1, 16)
colf <- gl(2, 8, 16, labels=1:0)
dat <- data.frame(y)
aggregate(dat[1], list(Row=rowf, Col=colf), mean)

  -Peter Ehlers
Jack Siegrist wrote: