Hi dear R-community,
I wonder if anyone has written code that will post-process the results of a
tapply to create a dataframe that includes the outcome, the factor by which
the function has been applied, and, if the factor is also an interaction,
then also the levels of contributing factors?
>From this
outcome <- tapply(data, A.B, function)
I'd like to arrange something like this:
A B A.B outcome
1
2
3
4
I have the following example solution, which seems clumsy, relying on
dissecting the labels of the tapply result. I don't think I've
harvested
this from anyone else, but I apologize if I have.
======================================================================
x <- factor(c(rep("B",4), rep("A",4)))
y <- factor(c(1,2,1,2,1,2,1,2))
z <- rnorm(8)
data <- as.data.frame(cbind(x, y, z))
data$x.y <- interaction(x,y)
my.test <- tapply(data$z, data$x.y, mean)
test <- as.data.frame(as.numeric(my.test))
names(test) <- "Mean"
nList <- function(list, n) list[[n]]
test$x <- factor(as.character(lapply(strsplit(row.names(my.test),
"\\."), nList, n=1)))
test$y <- factor(as.character(lapply(strsplit(row.names(my.test),
"\\."), nList, n=2)))
test$x.y <- factor(row.names(my.test))
test[,c(2:4,1)]
======================================================================
Any suggestions to simplify would be much appreciated.
Andrew
--
Andrew Robinson Ph: 208 885 7115
Department of Forest Resources Fa: 208 885 6226
University of Idaho E : andrewr at uidaho.edu
PO Box 441133 W : http://www.uidaho.edu/~andrewr
Moscow ID 83843 Or: http://www.biometrics.uidaho.edu
No statement above necessarily represents my employer's opinion.