I would like to summarise or aggregate tables without dropping empty levels. I wonder if anyone has any ideas on this?
As an example, Here is a data frame
df1<-data.frame(Method=c(rep("A",3),rep("B",2),rep("C",4)),
Type=c("Fast","Fast","Medium","Fast","Slow","Fast","Medium","Slow","Slow"),
Measure=c(1,1,2,1,3,1,1,2,2))
Two approaches using base and doBy
package.
#base
aggregate(Measure~Method+Type,data=df1,FUN=length)
require(doBy)
summaryBy(Measure~Method+Type,data=df1,FUN=length)
They both give the same results sorted differently, but the issue is that I would like all combinations of Method and Type and missing measures inserted as NAs. Or all levels of both my factors must be maintained.
df1$Type
df1$Method
Maybe plyr
has something, but I don't know how that works.
Thanks for your answers. I think all of them works to give the result. But the comment by Mark Heckmann with this code
ddply(df1, .(Method, Type), summarise, Measure=length(Measure), .drop=F)
seems to give a nice clean output dataframe with good header and with minimal code. On the downside, it needs additional package.