I want to parallelize the below code in R. It's a nested for loop.
for (i in 1:nrow(my_dataset_preprocessed)){
for (j in 1:ncol(my_dataset_preprocessed)){
my_dataset_preprocessed[i,j] = min( my_dataset_preprocessed[i,j], 0.1 )
}
}
I am trying the below code using doParallel
library(foreach)
library(doParallel)
registerDoParallel(detectCores())
clusterExport(cl, "my_dataset")
threshold_par <- function (X) {
co <- foreach(i=1:nrow(X)) %:%
foreach (j=1:ncol(X)) %dopar% {
co = min( X[i,j], 0.1 )
}
matrix(unlist(co), ncol=ncol(X))
}
system.time(threshold_par(my_dataset))
But I am getting the following error:
Error in { : task 1 failed - "invalid 'type' (list) of argument"
Is there any better way to parallelize this code (may be using parLapply)? If not, how do I fix the above code?
lapply(my_dataset_preprocessed, function(x) pmin(x, 0.1))would be simpler to do.my_dataset[my_dataset > 0.1] <- 0.1