R-提高采样函数速度
假设我有以下数据结构:R-提高采样函数速度,r,R,假设我有以下数据结构: sl_sev_disbn <- data.frame("lower_band" = c(0,10e6,20e6,30e6,0,0,0), "upper_band" = c(10e6,20e6,30e6,40e6,0,0,0), "prob" = c(0.56521739,0.34782609,0.08212560,0.00483092,0,0,0),
sl_sev_disbn <- data.frame("lower_band" = c(0,10e6,20e6,30e6,0,0,0),
"upper_band" = c(10e6,20e6,30e6,40e6,0,0,0),
"prob" = c(0.56521739,0.34782609,0.08212560,0.00483092,0,0,0),
"band" = c(1,2,3,4,5,6,7))
sl_sev_disbn利用runif
是矢量化的这一事实
sl_sev_disbn 37672.677 38327.886 60730.445 100
#> 89.284 92.444 2689.974 100
由(v0.3.0)于2019年10月16日创建
rsuper_lrg_disbn <- function(n = 1,df){
band_sample <- sample(x = df$band, size = n, prob = df$prob,replace=TRUE)
vals <- c()
for (band in band_sample){
filt_df <- df[df$band == band,] #filter to randomly selected band
loss <- runif(1,min=filt_df$lower_band,max=filt_df$upper_band)
vals <- c(vals,loss)
}
return(vals)
}