There's a much shorter way. You don't need that ugly h() with all those $
and potential for bugs !
Using the original f :
dt[,lapply(.SD,f),by=key(dt)]
grp1 grp2 grp3 a b d
xxx 1.00 81.00 161.00
xxx 10.00 90.00 17
Hi:
For this particular task, the aggregate() function and the doBy package
provide nicely formatted output, but you may have to do some renaming.
Let's try a more expansive toy example with which one can do a bit more.
df <- data.frame(grp1 = rep(c('x', 'y'), each = 40),
grp2
On Feb 7, 2011, at 11:29 PM, Al Roark wrote:
I'd like to summarize several variables in a data frame, for
multiple groups, and store the results in a data.frame. To do so,
I'm using by(). For example:
df<-
data
.frame
(a
=1:10,b=11:20,c=21:30,grp1=c("x","y"),grp2=c("x","y"),grp3=c("x
I'd like to summarize several variables in a data frame, for multiple groups,
and store the results in a data.frame. To do so, I'm using by(). For example:
df<-data.frame(a=1:10,b=11:20,c=21:30,grp1=c("x","y"),grp2=c("x","y"),grp3=c("x","y"))
dfsum<-by(df[c("a","b","c")], df[c("grp1","grp2","grp
4 matches
Mail list logo