Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R-提高采样函数速度_R - Fatal编程技术网

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)
}