Reputation: 683
I have a loop that iterates from 2 to a specified value(i.e. ,columnCount). The value of i is crucial as all computation that takes place inside the loop is dependent on the value of i.
Loop snippet:
> x1=runif(900000,9999,90999)
> x2=runif(900000,0,9)
> x3=runif(900000,5000,80000)
> y=rep(0:1,450000)
> data=data.frame(y,x1,x2,x3)
> dim(data)
[1] 900000 4
> columnCount = ncol(data)
> yVar = names(data[1])
for (i in 2:columnCount) {
xVar[i] = names(data[i])
result <- smbinning(df=data,y=yVar,x=xVar[i],p=0.05)
}
Note: Y column is always constant,while x columns iterates by 1 in every step(Actual data frame has 250+ columns).How do i translate this to so that I can use:
library(foreach)
library(doParallel)
foreach(icount(iters)) %dopar% {
Upvotes: 0
Views: 426
Reputation: 2715
How about using the mclapply from the parallel package. Something like the below for example:
require(smbinning) # caveat: I never used this package
data(iris)
names(iris) <- gsub("\\.","",names(iris)) # didn't like dots
mclapply(2:NCOL(iris), function(varb)
smbinning(df = iris[,c(1,varb)],
y = names(iris[,c(1,varb)])[1],
x = names(iris[,c(1,varb)])[2],
p = 0.05))
Upvotes: 2