我有疑问future(),doFuture()用法.
我想N并行运行计算(使用foreach ... %dopar%) - N我的机器上有多少核心.为此,我使用future:
library(doFuture)
registerDoFuture()
plan(multiprocess)
foreach(i = seq_len(N)) %dopar% {
foo <- rnorm(1e6)
}
Run Code Online (Sandbox Code Playgroud)
这就像一个魅力,因为我N并行运行计算.但是我需要实现另一个使用大量内核的分析步骤(例如,N).这是代码的样子:
foreach(i = seq_len(N)) %dopar% {
foo <- rnorm(1e6)
write.table(foo, paste0("file_", i, ".txt"))
# This step uses high number of cores
system(paste0("head ", "file_", i, ".txt", " > ", "file_head_", i, ".txt")
}
Run Code Online (Sandbox Code Playgroud)
我在运行多个rnorm与head并行,但由于head使用了大量的内核(让我们假设这个)我的分析卡住.
题:
如何使用并行运行部分代码future?(如何仅rnorm并行运行然后head …
我正在传递一个列表,map并希望返回一个带有合并名称的data.frame对象.
例如:
library(tidyverse)
library(broom)
mtcars %>%
split(.$vs) %>%
map_df(~ tidy(lm(mpg ~ cyl, .)))
term estimate std.error statistic p.value
1 (Intercept) 36.926733 3.690822 10.005017 2.727754e-08
2 cyl -2.728218 0.490297 -5.564419 4.272958e-05
3 (Intercept) 41.940000 5.778467 7.257981 1.003636e-05
4 cyl -3.802500 1.240052 -3.066404 9.781943e-03
Run Code Online (Sandbox Code Playgroud)
如何在其中提取名称(vs组)map并将其作为结果中的附加列添加,如下所示:
term estimate std.error statistic p.value GROUP
1 (Intercept) 36.926733 3.690822 10.005017 2.727754e-08 0
2 cyl -2.728218 0.490297 -5.564419 4.272958e-05 0
3 (Intercept) 41.940000 5.778467 7.257981 1.003636e-05 1
4 cyl -3.802500 1.240052 …Run Code Online (Sandbox Code Playgroud)