Abdel
Abdel

Reputation: 6106

R - converting a list into a data frame

I have a list that consists of lists that all contain four elements. It looks like this:

> summary(allMorans)
                      Length Class  Mode
trait1                 4      -none- list
trait2                 4      -none- list
trait3                 4      -none- list
traitB                 4      -none- list
traitX                 4      -none- list

The four elements are the result of a certain statistical test and contain the elements "observed", "expected", "sd", "pvalue".

If I do this:

MoransResults <- as.data.frame(allMorans)

I get a single very long row that looks like this:

 trait1.observed    trait1.expected    trait1.sd      trait1.pvalue       trait2.observed    trait2.expected   trait2.sd      trait2.pvalue    ...etc  
 0.1                0.2                0.01           0.09                0.3                0.2               0.01           0.07             ...etc

However, I need to turn this list of lists into a dataframe that looks like this:

TRAIT    observed   expected   sd     pvalue
trait1   0.1        0.2        0.01   0.09
trait2   0.3        0.2        0.01   0.07
trait3   0.1        0.1        0.01   0.55
traitB   0.2        0.2        0.01   0.49
traitX   0.3        0.2        0.01   0.07

How do I achieve this?

UPDATE:

As requested in the comments, here is some example data:

> dput(allMorans)
structure(list(trait1 = structure(list(observed = -0.00820649454281412, 
expected = -0.0001000100010001, sd = 6.44860382275647e-05, 
p.value = 0), .Names = c("observed", "expected", "sd", "p.value"
)), trait2 = structure(list(observed = -0.16378930443073, expected = -0.0001000100010001, 
sd = 6.44860728086603e-05, p.value = 0), .Names = c("observed", 
"expected", "sd", "p.value")), trait3 = structure(list(observed = -0.348047732487769, 
expected = -0.0001000100010001, sd = 6.44872069930741e-05, 
p.value = 0), .Names = c("observed", "expected", "sd", "p.value"
))), .Names = c("trait1", "trait2", "trait3"))

Upvotes: 0

Views: 186

Answers (3)

Santosh M.
Santosh M.

Reputation: 2454

Another alternative:

t(simplify2array(allMorans))

#           observed     expected             sd   p.value
#trait1 -0.008206495  -0.00010001   6.448604e-05      0      
#trait2 -0.1637893    -0.00010001   6.448607e-05      0      
#trait3 -0.3480477    -0.00010001   6.448721e-05      0  

Upvotes: 4

Maurits Evers
Maurits Evers

Reputation: 50678

In base R (no additional libraries necessary)

do.call(rbind, lapply(allMorans, as.data.frame))
#           observed    expected           sd p.value
#trait1 -0.008206495 -0.00010001 6.448604e-05       0
#trait2 -0.163789304 -0.00010001 6.448607e-05       0
#trait3 -0.348047732 -0.00010001 6.448721e-05       0

Upvotes: 3

www
www

Reputation: 39154

A solution using the tidyverse package. as.tibble convert each element in the list to be a tibble. map_dfr can then combine all these tibbles to form the final output.

library(tidyverse)

dt <- allMorans %>% map_dfr(as.tibble, .id = "TRAIT")
dt
# # A tibble: 3 x 5
#    TRAIT     observed    expected           sd p.value
#    <chr>        <dbl>       <dbl>        <dbl>   <dbl>
# 1 trait1 -0.008206495 -0.00010001 6.448604e-05       0
# 2 trait2 -0.163789304 -0.00010001 6.448607e-05       0
# 3 trait3 -0.348047732 -0.00010001 6.448721e-05       0

Upvotes: 1

Related Questions