%do% 比。 %dopar%, %dopar% 没有做任何改变,没有警告

%do% Vs. %dopar%, %dopar% does not make any changes, no warnings

(没有用于 r-parallel-processing 或 R 的 foreach 包的标签,如果有我会在这里标记它们。欢迎提供标签建议)。

我有一个数据框 "training_data" 和一个向量 "cats"(用于分类数据)。

猫看起来像 c("fruits", "vegetables", "meats")

我想遍历训练数据中的每只猫,并用 "Other" 替换任何低频级别。

这个有效:

library(foreach)
foreach(c = cats) %do% { # not parallel processing
  print(c)
  freqs <- table(training_data[c])
  low_freqs <- names(which(freqs < 20000))
  training_data[c][[1]] <- ifelse(training_data[c][[1]] %in% low_freqs, "Other", training_data[c][[1]])
  return(NULL) # otherwise spits out the whole thing
}

在每次迭代中,第一行 print(c) 输出正在操作的矢量猫的值,我在控制台中看到它:

"fruits" "vegetables" "meats"

在这些猫之后,3 个 NULL 实例被打印到终端,预期是由于循环中的最后一行。当我随后检查我的数据框 training_data 时,分类变量已按预期进行了转换。频率小于20k的任何级别已被其他替换。

但是,如果我尝试使用并行:

library(foreach)
foreach(c = cats) %dopar% { # parallel (I have 8 cores)
  print(c)
  freqs <- table(training_data[c])
  low_freqs <- names(which(freqs < 20000))
  training_data[c][[1]] <- ifelse(training_data[c][[1]] %in% low_freqs, "Other", training_data[c][[1]])
  #return(NULL) # otherwise spits out the whole thing
}

所发生的只是将 NULLS 打印到控制台。训练数据未转换,控制台中未显示 print(c)。

为什么只有 %do% 有效而 %dopar% 无效?

这是一种方法,使用了一些不同的示例数据。通过并行,迭代器的数据被复制到子进程中,因此限制复制的内容对于性能和内存使用很重要。


library(doParallel)

# make a cluster

cl <- makeCluster(2)
registerDoParallel(cl)

# sample data

cats <- c("fruits", "vegetables", "meats")
df <- read.csv(text = "
cat,n
fruits,1
fruits,2
vegetables,4
meats,5
", stringsAsFactors = FALSE)

# Use foreach to iterate over a split dataframe, so only the subset data
# will be copied into each parallel process. Specify .combine = rbind to
# bind the resulting dataframe into one dataframe parallel 

result <- foreach(dfs = split(df, df$cat), .combine = rbind) %dopar% {

 # Print to show the structure of each split - Won't print in parallel
 # because the output .. will go to the console output of each process


  cat("Inside...\n")
  print(dfs)

  # Derive a new column
  dfs$new_col <- ifelse(dfs$n > 2, ">2", "<=2")

  # Return the result without printing
  invisible(dfs)  
}

# Print the combined new dataframe
print(result)
#>          cat n new_col
#> 1     fruits 1     <=2
#> 2     fruits 2     <=2
#> 4      meats 5      >2
#> 3 vegetables 4      >2