如何在多个核心上使用R str_detect

时间:2015-10-13 09:48:19

标签: r string foreach multicore detect

我正在尝试使用foreach在R中为多个核心上的字符串检测运行一个循环,但我发现它只在一个核心上运行,而我无法在多个核心上运行它。

    library("parallel")
library("foreach")
library("doParallel")

#How to detect how many cores are on the server
detectCores()

cl <- makeCluster(3)
registerDoParallel(cl)

x<-c("Hello My Name is", "Happy Birthday", "Hi How are you today? My Name is walley", "Nice to meet you", "Best friends")
y<-c("Hello", "Birthday", "Hi", "Nice", "Best friends")

foreach(i = 1:length(y)) %dopar%{

print(i)
if (sum(str_detect(x,paste("\\b",as.character(y),"\\b", sep=""))) > 0){

    str_replace(x[i], as.character(y[which(str_detect(x[i],paste("\\b",as.character(y),"\\b", sep="")) == TRUE)]), "")
    }
}

write.csv(mycatalog, "Matching.csv")
getDoParWorkers()

stopCluster(cl)

1 个答案:

答案 0 :(得分:0)

我想循环不是并行执行的,因为你试图修改所有线程中的单个数组(x)。

尝试以下操作,看看它是否解决了您的问题

modifiedX = foreach(i = 1:length(y), .combine = c) %dopar%{

  stringCopy = x[i]

  if (sum(str_detect(x,paste("\\b",as.character(y),"\\b", sep=""))) > 0) {    
    str_replace(stringCopy, as.character (y[which(str_detect(x[i],paste("\\b",as.character(y),"\\b", sep="")) == TRUE)]), "")   
  }

  return(stringCopy)

}

print(modifiedX)