Skip to main content
  1. About
  2. For Teams
Asked
Viewed 189 times
Part of R Language Collective
0

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"))
2
  • 1
    It's not really enough to just describe what your data are like. You need to include actual example data in the question (e.g. dput(allMorans))
    dww
    –  dww
    2017-11-27 01:38:23 +00:00
    Commented Nov 27, 2017 at 1:38
  • 1
    Ok thanks, I will add that to the question! -edit- done!
    Abdel
    –  Abdel
    2017-11-27 01:45:04 +00:00
    Commented Nov 27, 2017 at 1:45

3 Answers 3

4

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  
Sign up to request clarification or add additional context in comments.

Comments

3

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

1 Comment

instead of lapply you can just use sapply and the identity function as t(sapply(allMorans,I)) though the best is to use ssimplify2array which is still in base R
1

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

Comments

Your Answer

Post as a guest

Required, but never shown

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.

Morty Proxy This is a proxified and sanitized view of the page, visit original site.