foreach和doparallel在R中运行没有问题,但没有得到任何正确的结果

foreach和doparallel在R中运行没有问题,但没有得到任何正确的结果,r,foreach,doparallel,R,Foreach,Doparallel,我正在尝试创建一个foreach,以便将拼写错误的单词替换为更大的数据帧。我的代码运行时没有问题,但我没有看到正确的结果。请参阅下面的示例,了解我的数据帧和我使用的代码 我有一个主数据框和一个数据框,用于查找和替换主数据框中预定义的拼写错误文本: #create main data frame df <- data.frame("Index" = 1:7, "Text" = c("Brad came to dinner with us tonigh.",

我正在尝试创建一个
foreach
,以便将拼写错误的单词替换为更大的数据帧。我的代码运行时没有问题,但我没有看到正确的结果。请参阅下面的示例,了解我的数据帧和我使用的代码

我有一个主数据框和一个数据框,用于查找和替换主数据框中预定义的拼写错误文本:

#create main data frame
df <- data.frame("Index" = 1:7, "Text" = c("Brad came to dinner with us tonigh.",
                                            "Wuld you like to trave with me?",
                                            "There is so muh to undestand.",
                                            "Sentences cone in many shaes and sizes.",
                                            "Learnin R is fun",
                                            "yesterday was Friday",
                                            "bing search engine"), stringsAsFactors = FALSE)

#create predefined misspelled data frame
df_r <- data.frame("misspelled" = c("tonigh", "Wuld", "trave", "muh", "undestand", "shaes", "Learnin"), 
                   "correction" = c("tonight", "Would", "travel", "much", "understand", "shapes", "Learning"))

library(DataCombine)
library(doParallel)
library(foreach)
no_cores <- detectCores()
cl <- makeCluster(no_cores[1]-1)
registerDoParallel(cl)

df_replacement <- foreach((df$Text), .combine = cbind) %dopar% {
  replacement = DataCombine::FindReplace(data = df, Var = "Text", replaceData = df_r,
                                             from = "misspelled", to = "correction", exact = FALSE)

  replacement
}
stopCluster(cl)
#创建主数据帧

df我想你在寻找这个:

df_replacement <- foreach(i = (rownames(df)), .combine = rbind) %dopar% {
  replacement = DataCombine::FindReplace(data = df[i,], Var = "Text", replaceData = df_r,
                                         from = "misspelled", to = "correction", exact = FALSE)

  replacement
}

df_replacement出于某些原因,我得到了与原始主数据框相同的句子,而且我不确定为什么结果列多次重复我也得到了一个数据框,但有14列,而它应该只包含2列。啊,我明白了。非常感谢你指出这一点。有趣的是,我重新启动了R Studio,得到了正确的句子。现在一切都很好。