1. ddply(.data, .variable,.fun,...)
.data: is the data to break up
.variable: the grouping variable used to break up the dataset.
.fun: summary function you want to use to each group. return a vector or dataframe
use subset() to select qualified subgroups
ddply(diamonds,.(color),subset,carat==min(carat))
use transform() for transformation
use colwise() to apply a function to selected column in the dataframe
colwise(function): use a function to dataframe, or selected columns of a dataframe
e.g. colwise(mean)(msleep)
numcolwise(): only used for numeric column
catcolwise(): only use for category column
colwise() combined with ddply(), to produce per-group summaries
ddply(msleep, .(vore),numcolwise(median),nam.rm=T)
smooth=function(df){
mod=gam(price~s(carat,bs="cs"),data=df)
grid=data.frame(carat=seq(0.2,2,length=50)
pred=predict(mod,grid,se=T)
grid$price=pred$fit
grid$se=pred$se.fit
grid}
smoothes=ddply(dense,.(color),smooth)
qplot(carat,price,data=smoothes,color=color,geom="line")