我将使用%dopar%
和foreach
,我需要合并输出。
将并行调用的函数具有一个列表,该列表对于每个调用具有恒定长度。但是,此列表元素的长度并不总是恒定的。
组合后,我希望结果尽可能简化,同时仍允许我识别每个元素的原始列表(迭代)。
这个更深层次的B / C长度不同,answers like this并不能让我在那里。以下是一些示例数据:
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)))
给出:
[[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
和do.call(Map, c(list, list(list1, list2, list3)))
给出:
[[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
编辑,正确答案应该是这样的(原谅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
我还应该指出,列表中的元素不一定是数字 - 它们可以是模型输出,例如来自jags()
。
使用c
获得第一部分(相对于我想要的),使用list
获得最后一部分。我如何充分利用这两个世界?
答案 0 :(得分:0)
我想我已经明白了 - 关键只是看sapply
和mapply
如何简化结果:simplify2array
。
我不确定无论列表中每个元素的类如何,这个答案都会起作用(尽管我添加了一个lm
对象来尝试测试它):
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)
正确导致:
[[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