简体   繁体   中英

Combine corresponding elements of lists into either vector or list

I am going to be using %dopar% and foreach , and I need to combine the outputs.

The function that will be called in parallel has as it's output a list which has a constant length for each call. However, the lengths of the elements of this list are not always constant.

After combining, I want the result to be as simplified as possible, while still allowing me to identify each element's list (iteration) of origin.

B/c lengths differ at this deeper level, answers like this don't quite get me there.

Here's some example data:

list1 <- list(rnorm(1), rnorm(1), rnorm(1), rnorm(8))
list2 <- list(rnorm(1), rnorm(1), rnorm(1), rnorm(8))
list3 <- list(rnorm(1), rnorm(1), rnorm(1), rnorm(14))

do.call(Map, c(c, list(list1, list2, list3))) gives:

    [[1]]
    [1] -0.2923462  0.4891224 -0.5080176

    [[2]]
    [1]  0.3229466  0.9511572 -0.9815504

    [[3]]
    [1] -1.160413  0.707568 -1.564874

    [[4]]
     [1] -1.13093146  0.06791923  0.65380844  1.01829862  0.47360903  0.68616334 -1.07166155 -1.54018814 -0.60860430  1.64524185  0.40222817 -0.54747627
    [13] -1.73420011  0.67861611  0.55527953  1.36454409  0.40215155 -0.65706184 -0.71008434 -1.11484886 -0.69811408 -0.45451101 -0.85574891 -0.79241329
    [25]  0.31018144 -0.03212242 -1.55192430 -2.19142725 -1.85528112  0.85204097

and do.call(Map, c(list, list(list1, list2, list3))) gives:

[[1]]
[[1]][[1]]
[1] -0.2923462

[[1]][[2]]
[1] 0.4891224

[[1]][[3]]
[1] -0.5080176


[[2]]
[[2]][[1]]
[1] 0.3229466

[[2]][[2]]
[1] 0.9511572

[[2]][[3]]
[1] -0.9815504


[[3]]
[[3]][[1]]
[1] -1.160413

[[3]][[2]]
[1] 0.707568

[[3]][[3]]
[1] -1.564874


[[4]]
[[4]][[1]]
[1] -1.13093146  0.06791923  0.65380844  1.01829862  0.47360903  0.68616334 -1.07166155 -1.54018814

[[4]][[2]]
[1] -0.6086043  1.6452418  0.4022282 -0.5474763 -1.7342001  0.6786161  0.5552795  1.3645441

[[4]][[3]]
 [1]  0.40215155 -0.65706184 -0.71008434 -1.11484886 -0.69811408 -0.45451101 -0.85574891 -0.79241329  0.31018144 -0.03212242 -1.55192430 -2.19142725
[13] -1.85528112  0.85204097

EDIT, Correct answer should like something like this (forgive the RNG):

part1 <- do.call(Map, c(c, list(list1, list2, list3)))
part2 <- do.call(Map, c(list, list(list1, list2, list3)))
correct <- list(part1[[1]], part1[[2]], part1[[3]], part2[[4]])
correct
[[1]]
[1]  1.80341685 -0.06408827  0.07004951

[[2]]
[1]  0.4389224 -0.1821140  0.2538133

[[3]]
[1]  0.008303713 -1.004631075  1.936738072

[[4]]
[[4]][[1]]
[1] -0.86790931  1.20414809  0.04373068 -0.49097606  1.12826503 -0.76263091 -0.93364770  0.13392904

[[4]][[2]]
[1] -1.0823008 -0.4382813  1.4328709 -0.8961412  0.8350054  1.4855032 -1.3800748  1.4300227

[[4]][[3]]
 [1]  0.02126034  0.30640618  0.49420442  0.72107997  0.97666620 -0.48049810  1.22227279 -1.00918452 -0.23290645 -1.27834163  2.55142878  1.07120297
[13]  1.37473759  0.72308135

I should also point out that the elements of the list are not necessarily numeric – they could be model output, eg, from jags() .

Using c gets the first part right (relative to what I want), and using list gets the last part right. How do I get the best of both worlds?

I think I just figured it out – the key was just looking at how sapply and mapply simplify their results: simplify2array .

I'm not sure this answer will work regardless of the class of each element of the list (although I added an lm object to try to test this):

list1 <- list(rnorm(1), rnorm(1), rnorm(1), rnorm(8), list(lm(y1~x1)))
list2 <- list(rnorm(1), rnorm(1), rnorm(1), rnorm(8), list(lm(y1~x1)))
list3 <- list(rnorm(1), rnorm(1), rnorm(1), rnorm(14), list(lm(y1~x1)))

lapply(do.call(Map, c(list, list(list1, list2, list3))), simplify2array)

which correctly results in:

[[1]]
[1] -0.3947090  0.3347808 -0.3404769

[[2]]
[1] -0.4661581  1.0141749  0.3178242

[[3]]
[1]  0.4460540 -0.3971673  0.7291202

[[4]]
[[4]][[1]]
[1]  0.15486131  0.04511161  0.79932793  0.31679677 -1.05818552 -0.59902937  0.05348751 -1.28561604

[[4]][[2]]
[1] -1.1898877 -0.9595261  1.2784798  0.6056794  0.2355697 -0.5116538 -1.0667602  2.1319707

[[4]][[3]]
 [1] -0.03475871  0.50329073 -1.25297549  0.75347700  0.30558110  0.39872038  0.62724542  0.14938488  0.42032236  0.20953381  1.26509289  0.47796645
[13]  0.33260481  1.10625794


[[5]]
[[5]][[1]]

Call:
lm(formula = y1 ~ x1)

Coefficients:
(Intercept)           x1  
     0.2366      -0.4091  


[[5]][[2]]

Call:
lm(formula = y1 ~ x1)

Coefficients:
(Intercept)           x1  
     0.2366      -0.4091  


[[5]][[3]]

Call:
lm(formula = y1 ~ x1)

Coefficients:
(Intercept)           x1  
     0.2366      -0.4091  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM