McCandar McCandar - 4 years ago 113
R Question

Merge multidimensional lists into one

I have a list like

[[1]]
NULL

[[2]]
NULL

[[3]]
i index
[1,] 3 6359

(...)

[[7]]
NULL

[[8]]
i index
[1,] 8 1609
[2,] 8 2197
[3,] 8 3045

[[9]]
i index
[1,] 9 1342
[2,] 9 5831

[[10]]
NULL


and created with

temp <- parSapply(cl,seq_along(source),function(i){
index <- which(target[,col]==source[i])
if(length(index)!=0)
cbind(i,index)
})


I want to turn this list into another list or data frame like

i index
3 6359
8 1609
8 2197
8 3045
9 1342
9 5831


in a way that I quickly get entire column with temp[[1]] or temp[,1] etc. It would be nice without rbind and with the fastest possible (parallel would be awesome) way.

Answer Source

since your code is throwing errors, i have created a sample list:

l = list(a = matrix(c(a = 1, b = "a"), ncol = 2), 
         b = matrix(c(a = c(2,3), b=c("a","b")), ncol = 2), 
         c = NULL)
#l
#$a
#     [,1] [,2]
#[1,] "1"  "a" 
#
#$b
#     [,1] [,2]
#[1,] "2"  "a" 
#[2,] "3"  "b" 
# 
#$c
#NULL

l <- lapply(l,  as.data.frame) # converting each `matrix` to `data.frame` since bind_rows works on this only
#l
#$a
#  V1 V2
#1  1  a
# 
#$b
#  V1 V2
#1  2  a
#2  3  b
# 
#$c
#data frame with 0 columns and 0 rows

library(dplyr)
bind_rows(l1)
#  V1 V2
#1  1  a
#2  2  a
#3  3  b
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download