I found something very confusing when I use multiple processing to modify values in R data.table.
I tried to modify value in place by using a function. It works well using one core, and the values in data.table were successfully changed. But when I used multiple cores, it failed to change the value in data.table.
That makes me very confused. Anyone know why?
library(data.table)
library(parallel)
aa <- as.data.table(iris)
aa[,tt:=0]
# modify aa$tt in place
main <- function(x){
#set(aa,x,6L,5)
aa[x,tt:=5]
return(NULL)
}
# aa$tt changed
mclapply(1:nrow(aa), main, mc.cores = 1)
# aa$tt unchanged
mclapply(1:nrow(aa), main, mc.cores = 2)