Sample using start and end values within a loop in R

前端 未结 3 2050
庸人自扰
庸人自扰 2021-01-27 05:31

I am trying to sample between a range of values as part of a larger loop in R. As the loop progresses to each row j, I want to sample a number between the value giv

3条回答
  •  逝去的感伤
    2021-01-27 05:52

    You might not need to loop through. If you want need is something between start and end, it's almost equivalent to sampling something between 0-1 and multiplying that by the range.

    df %>% mutate(sampled = start + round((end-start)*runif(nrow(.))))
    

    Regarding the updating, dependencies you mentioned in your comment: sounds a bit complicated. Quick thought: Might be faster to sample a lot of times and choose one that fits your criteria.

提交回复
热议问题