Re: [R] functions and multiple levels
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. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] functions and multiple levels
Hi Chris, Try ?ave will help you. Anyway, I guess you are computing a statistic. strs -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 df - read.table(textConnection(strs),header=T) df$mean1 - ave(df$observation, df$level.1, FUN = function(x) mean(x)) df$mean2 - ave(df$observation, df$level.2, FUN = function(x) mean(x)) df.mean - merge(unique(df[,c(1,4)]),unique(df[,c(2,5)]),by.x = level.1, by.y = level.2) df.mean$square - (df.mean$mean1 - df.mean$mean2)^2 Regards, Wu - A R learner. -- View this message in context: http://r.789695.n4.nabble.com/functions-and-multiple-levels-tp2329935p2330001.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] functions and multiple levels
On Wed, 18 Aug 2010, chris20 wrote: Hi, I am trying to write a function; I want to subtract the mean of each class in level 2 from the mean of each class in level 1 and square the answer, eg. 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 (mean(1$level.1) - mean(1$level.2))^2 (mean(1$level.1) - mean(2$level.2))^2 etc. Chris, Almost always best to break things into little pieces, like this: # read data (copy above to clipboard) dat - read.table(clipboard,head=T, +colClasses=c('factor','factor','numeric')) # means of level.1 m1 - coef(lm(observation~0+level.1,dat)) # means of level.2 m2 - coef(lm(observation~0+level.2,dat)) # all differences outer( m1, m2, '-') level.21 level.22 level.23 level.24 level.110.075 -0.075 -0.325 -0.075 level.120.2750.125 -0.1250.125 # all differences squared outer( m1, m2, '-')^2 level.21 level.22 level.23 level.24 level.11 0.005625 0.005625 0.105625 0.005625 level.12 0.075625 0.015625 0.015625 0.015625 HTH, Chuck I want to write a function because I have lots of levels and lots of different observations. I thought this should be easy (it's my first attempt at writing a function) but I just can't work it out! Thanks Chris -- View this message in context: http://r.789695.n4.nabble.com/functions-and-multiple-levels-tp2329935p2329935.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Charles C. Berry(858) 534-2098 Dept of Family/Preventive Medicine E mailto:cbe...@tajo.ucsd.edu UC San Diego http://famprevmed.ucsd.edu/faculty/cberry/ La Jolla, San Diego 92093-0901 __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.