[R] functions and multiple levels

chris20 bop07crb at sheffield.ac.uk
Thu Aug 19 17:27:34 CEST 2010



Thanks for the replies, it wasn't quite what I wanted but it has given me
some more code for working out averages.

I have managed to construct something that nearly works........

level.1      level.2      observation 
    1             1                 0.5 
    1             1                 0.2 
    1             2                 0.6 
    1             2                 0.4 
    2             3                 0.8 
    2             3                 0.7 
    2             4                 0.6 
    2             4                 0.4 

# read data (copy above to clipboard) 
 dat <- read.table("clipboard",head=T) 

mean.lev<- function(a, b, c) {
	lev.1<-rep(tapply(c, a, mean),each=2)
	lev.2<-tapply(c, b, mean)
	ss.lev<-(lev.1-lev.2)^2
	ss.lev.n<-ss.lev*(length(c)/max(b))
	return(as.matrix(ss.lev,ss.lev.n,nrow=max(b)))
	
}
mean.lev(level.1, level.2, observation)

The only problem is that it won't print both ss.lev and ss.lev.n, does
anyone know to to get it to print both?

I want it in a function as above because I have lots of data sets to apply
it to.

Thanks Chris 

-- 
View this message in context: http://r.789695.n4.nabble.com/functions-and-multiple-levels-tp2329935p2331403.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list