Reputation: 31
I have the following code that in R that I'm trying to speed up since I know loops are slower in R. Is there a way to do this R without having to use nested loops.
# initialize 2 vectors of length 10,000
totalNum <- rep(0,10000)
totalAmt <- rep(0,10000)
values <- sample(200:5000,150000, replace = TRUE)
chances <- # similar to f in length and contains values between 0 and 1
# loop over length of a vector
for (i in 1:150000){
# value between 200-5000
value <- values[i]
# value of number between 0 and 1
chance <- chances[i]
# loop over vectors created
for (j in 1:10000){
# run test
dice <- runif(1)
if (dice < chance){
totalnum[j] <- totalNum[j] + 1
totalAmt[j] <- totalAmt[j] + value
}
}
}
I've been trying to use lapply or mapply but doesn't seem like it will work for this situation.
Upvotes: 0
Views: 1023
Reputation: 12713
size = 150000
for values and chances vectors
library('data.table')
df1 <- data.table(totalNum = rep(0,10000),
totalAmt = rep(0,10000))
values <- sample(200:5000,150000, replace = TRUE)
chances <- runif(n=150000, min=1e-12, max=.9999999999)
invisible( mapply( function(value, chance){
df1[runif(10000) < chance, `:=` (totalNum = totalNum + 1, totalAmt = totalAmt + value)]
return(0)
}, value = values, chance = chances) )
On my system, this code completes with the following time using the system.time()
function.
# user system elapsed
# 252.83 43.93 298.68
Upvotes: 2
Reputation: 24079
lapply
and mapply
are just hidden loops with marginal improvement over a for
loop. For significant improvement you need to use the vectorized forms of the functions.
The inner loop is easily replaced with a vectorized form:
#generate all of the rolls
dice<-runif(10000)
#identify the affected index
dicelesschance<-which(dice<chance)
#update the values
totalNum[dicelesschance]<-totalNum[dicelesschance] + 1
totalAmt[dicelesschance]<-totalAmt[dicelesschance] + value
This should have a noticeable improvement on performance.
Upvotes: 1