[R] tapply output as a dataframe

jim holtman jholtman at gmail.com
Mon Apr 13 18:52:56 CEST 2009


> do.call(rbind,a)
        [,1]       [,2]      [,3]       [,4]
1 -0.7871502 -0.4437714 0.4011135 -0.2626129
2 -0.9546515  0.2210001 0.8666616  0.1245766
3 -0.5389725 -0.2750984 0.6655951 -0.1873485
4 -0.8176898 -0.1844181 0.4737187 -0.2688996


On Mon, Apr 13, 2009 at 12:41 PM, Dan Dube <ddube at advisen.com> wrote:
> 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!
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem that you are trying to solve?




More information about the R-help mailing list