[R] how to "apply" functions to unbalanced data in long format by factors......cant get "by" or "aggregate" to work

hadley wickham h.wickham at gmail.com
Thu Mar 8 19:30:18 CET 2007


> Hello R-users
> The help I received from Petr helped me created this solution to my problems.
>
> t1<-with(mydata ,aggregate(mydata$Y,
> list(mydata$time,mydata$treatment, mydata$expREP, mydata$techREP) ,
> median, na.rm=T)) ### find median by factors ####
>
> colnames(t1)<-c("time","treatment","expREP","techREP","Y50") ### column name ##
>
> newdata<-merge(mydata, t1, by.x= names(mydata)[2:5],
> by.y=names(t1)[1:4], all=T)
>

Another way is to use the reshape package, http://had.co.nz/reshape

library(reshape)
molten <- melt(mydata, m="log2Abun")

cast(molten, time + treatment +  expREP + techREP ~ ., median)

# You can also create many other "shapes" easily:
cast(molten, expREP + techREP ~ time + treatment , median)
cast(molten, expREP + techREP ~ time + treatment , median, margins=TRUE)

Hadley



More information about the R-help mailing list