vka
vka

Reputation: 316

Convert the Multiple Lists into one single data Frame in r

I have Multiple lists which look like below: I am facing a problem in converting those list into Data Frame in R

I have written few codes in R to converted list into data Frame but is given a wrong data frame

### the first list
b1

[[1]]
[1] "math"

[[2]]
[1] "write"

[[3]]
[1] 0.6439442

[[4]]
[1] 0.2939780 0.8416635

[[5]]
     2.5%     97.5% 
0.5517048 0.7156524 


## second list
b2
[[1]]
[1] "math"

[[2]]
[1] "read"

[[3]]
[1] 0.663126

[[4]]
[1] 0.3242639 0.8511580

[[5]]
     2.5%     97.5% 
0.5716105 0.7384209 

3 third list
b3

[[1]]
[1] "science"

[[2]]
[1] "write"

[[3]]
[1] 0.5812035

[[4]]
[1] 0.1995946 0.8097306

[[5]]
     2.5%     97.5% 
0.4791782 0.6721795 



like this have more 100 lists which I am trying to converting into a data frame.

for that, I written a code to convert the multiple lists into in Data Frame in R

Have combined all list into a single list which will be easy to convert in a data frame

list_full <- list(b1,b2,b2,b4)
f1 <- lapply(list_full,function(x){

           max.length<-max(sapply(x, length))
           data.frame(sapply(x, function(v) {c(v, rep(NA, max.length-length(v)))}), stringsAsFactors = FALSE)
           })


library(dplyr)
j<-bind_rows(f1, .id = 'ID')

we execute the code I getting a wrong data Frame as given below where the second value is stored in the separated row. if you see it you will know it

    ID  X1      x2       x3     x4            x5
1   1   math    write   0.64394 0.2939780   0.55170476
2   1   NA      NA     NA       0.841663    0.7156523
3   2   math    read    0.6631  0.324263    0.57161
4   2   NA      NA       NA     0.851157    0.7384209
5   3   math    read    0.6631  0.3242      0.5716104
6   3   NA       NA     NA      0.85115     0.7384
7   4   science read    0.644   0.2963      0.552
8   4   NA      NA      NA      0.842       0.72

this is problem i am currently facing so please provide me better code to slove this problem

The final data frame should look like this:


x1         x2        x3               x4               x5
math       wirte     0.643    (0.29, 0.84)     (0.55, 0.72)
math       read       0.66    (0.32 0.851)     (0.57 0.74)
science    write     0.581    (0.19 0.80)       (0.47 0.67)

Upvotes: 2

Views: 264

Answers (1)

thelatemail
thelatemail

Reputation: 93938

Here's an attempt using some Mapping of the lists and some paste-ing together of the results.

allb <- do.call(Map, c(rbind, list(b1,b2,b3)))
allb <- lapply(allb, function(x) {
  if(ncol(x) > 1) paste0("(", apply(x,1,paste,collapse=", "), ")") else
                  x 
})
as.data.frame(allb, col.names = paste0("x", seq_along(allb)))
#       x1    x2        x3                     x4                     x5
#1    math write 0.6439442  (0.293978, 0.8416635) (0.5517048, 0.7156524)
#2    math  read 0.6631260  (0.3242639, 0.851158) (0.5716105, 0.7384209)
#3 science write 0.5812035 (0.1995946, 0.8097306) (0.4791782, 0.6721795)

Where the source lists were:

b1 <- list(
  "math",
  "write",
  0.6439442,
  c(0.2939780, 0.8416635),
  setNames(c(0.5517048, 0.7156524), c("2.5%","97.5%"))
)

b2 <- list(
  "math",
  "read",
  0.663126,
  c(0.3242639, 0.8511580),
  setNames(c(0.5716105, 0.7384209), c("2.5%","97.5%"))
)

b3 <- list(
  "science",
  "write",
  0.5812035,
  c(0.1995946, 0.8097306),
  setNames(c(0.4791782, 0.6721795), c("2.5%","97.5%"))
)

Upvotes: 2

Related Questions