darkage
darkage

Reputation: 857

Convert a list from tapply(.) to data.frame in R

I have the following code

t <- tapply(z[,3],z[,1],summary)
# > t
# $AUS
#     Min.  1st Qu.   Median     Mean  3rd Qu.     Max. 
# -0.92420 -0.57920  0.08132 -0.13320  0.35940  0.39650 
# 
# $NZ
#     Min.  1st Qu.   Median     Mean  3rd Qu.     Max. 
# -1.80700 -0.98980 -0.98310 -0.57470  0.02292  0.88330 

I want to have it displayed as

            Min.  1st Qu.   Median     Mean  3rd Qu.     Max.
Aus     -0.92420 -0.57920  0.08132 -0.13320  0.35940  0.39650
NZ      -1.80700 -0.98980 -0.98310 -0.57470  0.02292  0.88330 

How can it be done in R?

Upvotes: 2

Views: 1480

Answers (2)

BrodieG
BrodieG

Reputation: 52647

You should consider using data.table for this type of analysis. Here, I created z as a two column data frame with columns Country and GDP:

library(data.table)
data.table(z)[, as.list(summary(GDP)), by="Country"]

produces

   Country     Min. 1st Qu. Median   Mean 3rd Qu.   Max.
1:     AUS 0.005469  0.1822 0.4193 0.4182  0.6032 0.9437
2:      NZ 0.034400  0.3248 0.4838 0.5269  0.7648 0.9968

data.table treats list return values as containing columns.

Upvotes: 2

CHP
CHP

Reputation: 17189

Try

do.call(rbind, t)

It will rbind all the dataframes from the list t

Btw. specifically to what you are doing, you can also achieve it by simply usin

t(sapply(z[, c(1,3)], summary))

Upvotes: 5

Related Questions