[R] tapply output as a dataframe

Dan Dube ddube at advisen.com
Mon Apr 13 18:41:22 CEST 2009


i use tapply and by often, but i always end up banging my head against
the wall with the output.  

is there a simpler way to convert the output of the following tapply to
a dataframe or matrix than what i have here:

# setup data for tapply
dt = data.frame(bucket=rep(1:4,25),val=rnorm(100))
fn = function(x) { 
  ret =
c(unname(quantile(x,probs=seq(.25,.75,.25),na.rm=T)),mean(x,na.rm=T))
}
a = tapply(dt$val,dt$bucket,fn)

# i seem to be doing way more work than i should here....
n = names(a)
mat = NULL
for (i in 1:length(n)) {
  mat = rbind(mat,unname(unlist(a[i])))
}
row.names(mat) = n

thank you!




More information about the R-help mailing list