[英]Using a nested loop in R alternative
我在 R 中有以下代码,我正在尝试加快速度,因为我知道 R 中的循环较慢。有没有办法在不必使用嵌套循环的情况下执行此 R。
# 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
}
}
}
我一直在尝试使用 lapply 或 mapply 但它似乎不适用于这种情况。
值和机会向量的size = 150000
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) )
在我的系统上,此代码使用system.time()
函数在以下时间完成。
# user system elapsed
# 252.83 43.93 298.68
lapply
和mapply
只是隐藏循环,对for
循环进行了边际改进。 为了显着改进,您需要使用函数的矢量化形式。
内部循环很容易用矢量化形式替换:
#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
这应该对性能有明显的改进。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.