r-并行处理-组合结果



我已经管理好并行运行下面的任务:

require(data.table)
library(parallel)
library(foreach)
library(doParallel)

# create computing cluster
cl <- makeCluster(detectCores() - 1)
registerDoParallel(cl, cores = detectCores() - 1)

# dummy df
df <- data.table(text = c('apple pie', 'dolphin', 'orange juice')); df
text
1:    apple pie
2:      dolphin
3: orange juice
# target string
x <- paste0("\b", c('apple', 'poo'),"\b", collapse = "|")
y <- paste0("\b", c('orange', 'kiwi'),"\b", collapse = "|")
z <- list(x,y); z
> z
[[1]]
[1] "\bapple\b|\bpoo\b"
[[2]]
[1] "\borange\b|\bkiwi\b"
# initialise
df[, flag_matched := 0 ]
# parallel computing - flag rows with a match
a = foreach(i = seq_along(z)
, .packages = c("data.table")
, .combine = rbind
) %dopar%
{
df[, flag_matched := flag_matched + as.numeric(grepl(z[[i]], text, perl=T)) ]
}
# stop hoarding cluster
stopCluster(cl)

然而,我目前在函数foreach中有rbind作为combine自变量,因此,导致行数=nrow(df(*循环数:

> a
text flag_matched
1:    apple pie            1
2:      dolphin            0
3: orange juice            0
4:    apple pie            0
5:      dolphin            0
6: orange juice            1

然后我可以做df[, .(sum(flag_matched)), text]。然而,还有其他方法可以组合结果吗?

您可以这样做:

library(data.table)
library(doParallel)
# create computing cluster
registerDoParallel(cl <- makeCluster(detectCores() - 1))
# dummy df
df <- data.table(text = c('apple pie', 'dolphin', 'orange juice')); df
# target string
x <- paste0("\b", c('apple', 'poo'), "\b", collapse = "|")
y <- paste0("\b", c('orange', 'kiwi'), "\b", collapse = "|")
z <- list(x,y); z
# parallel computing - flag rows with a match
a <- foreach(z_i = z) %dopar% {
grepl(z_i, df$text, perl = TRUE)
}
df$flag_matched <- Reduce(`+`, a)
# stop hoarding cluster
stopCluster(cl)

最新更新