[R] Construction of a "mean" contengency table

From: Pascal Boisson <Pascal.Boisson_at_scri.ac.uk>
Date: Tue 26 Apr 2005 - 23:12:30 EST

Hi List,

Say I have a data.frame "DF" with 6 columns, 3 factors and 3 variables, with different number of repetitions for each combination of factors. I would like to build, for two given factors, a matrix per variable, containing in each cell the mean or sd for a given couple of factors.

I have managed to get to the result I wanted step by step, but I would like to have it in a function. I have not found what I was looking for so I decided to try to write my own function, using aggregate( ) to generate the mean values as a dataframe, and then display this as a matrix.
I'd like it to be general enough so that I could use it in many cases.

>From what I have tried and understood, table( ) cannot do what I want (no mean or sd ...) neither does xtabs( ), i.e. asking for a formula not a function to passed as an argument ...

My final aim would be to plot this with persp( ), and it seems that persp( ) must be fed with 2 dimensional arrays ...

Below is what I have tried to write, but it is not working. It might be awkward, but I think it can work ... I think it is a problem about correctly receiving and passing arguments in the different function calls.
Do you think there would be a smarter way of doing this ? Or can you see what is going wrong with this ?

Any advice will be welcome

Pascal Boisson

matrixation<-function(DF, var, factorlist, FUN, condition)

## DF is the original dataframe with raw results from my experiments
## var is the var that I want to print
## eg. DF$var1
## factorlist is a list of the two factor I want to mean against (but
I'm noot sure List is the best form for this argument),
## eg. list(DF$fact1, DF$fact2)
## FUN would be the function I want to use during aggregation,
## eg. Mean( )
## condition would be a condition for building a subset
## eg. DF$fact3==1


sDF <- subset(DF, condition)
A <- aggregate(sDF, factorlist, FUN)

M <- matrix(data=NA, nrow = length(levels(factorlist[[1]])),

			   ncol = length(levels(factorlist[[2]])),
			   dimnames =
list(levels(factorlist[[1]]),levels(factorlist[[2]]) )

for (i in 1:length(A[,1])) 
	tPhii <- A$Group.1[i]
	tAppari <- A$Group.2[i]
	M[tPhii, tAppari] <- A$pest_max_number[i]

_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

\ This email is from the Scottish Crop Researc...{{dropped}}

R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Tue Apr 26 23:16:52 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:31:26 EST