Skip to content
Prev 169091 / 398506 Next

lapply and aggregate function

You could use ddply from the plyr package for this:

install.packages("plyr")
library(plyr)

ddply(myD, .(Light), transform,
  constraint = value > mean(value) & value < mean(value) * 1.5)

This applies the transform function to each subset defined by Light,
and then joins all the pieces back together in a single data frame.

You can use a similar approach for the other parts:

myD <- ddply(myD, .(Light), transform, meanLight = mean(value))
myD <- ddply(myD, .(Feed), transform, meanFeed = mean(value))
myD <- ddply(myD, .(Feed, Light), transform, meanFeedLight = mean(value))

Hadley