Dear Erik, Thank you very much. Indeed ave did the same job amazingly fast! I did not know the function before.
Many thanks to all R experts who answer to this mailing list, it's amazing how much help you offer to the newbies :) Kind regards, Stella On Tue, Jun 1, 2010 at 6:11 PM, Erik Iverson <er...@ccbr.umn.edu> wrote: > > > Stella Pachidi wrote: > >> Dear Erik and R experts, >> >> Thank you for the fast response! >> >> I include an example with the ChickWeight dataset: >> >> ap.dat <- ChickWeight >> >> matchMeanEx <- function(ind,dataTable,aggrTable) >> { >> index <- which((aggrTable[,1]==dataTable[["Diet"]][ind]) & >> (aggrTable[,2]==dataTable[["Chick"]][ind])) >> as.numeric(aggrTable[index,3]) >> } >> >> avgW <- aggregate(ap.dat[["weight"]], by = list(ap.dat[["Diet"]], >> ap.dat[["Chick"]]), FUN="mean") >> meanW <- sapply((1:length(ap.dat[,1])), FUN=matchMeanEx, ap.dat, avgW) >> ap.dat <- cbind (ap.dat, meanW) >> >> > > How about simply using ave. > > ap.dat$meanW <- ave(ap.dat$weight, list(ap.dat$Diet, ap.dat$Chick)) > -- Stella Pachidi Master in Business Informatics student Utrecht University email: s.pach...@students.uu.nl tel: +31644478898 [[alternative HTML version deleted]] ______________________________________________ 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.