Sample using start and end values within a loop in R

前端 未结 3 2051
庸人自扰
庸人自扰 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.

    0 讨论(0)
  • 2021-01-27 05:53

    First, put the data in a format that is easier to use with dput(df):

    df <- structure(list(ID = structure(1:14, .Label = c("a", "b", "c", 
        "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n"), class = "factor"), 
        start = c(25L, 36L, 23L, 15L, 21L, 43L, 39L, 27L, 11L, 21L, 
        28L, 44L, 16L, 25L), end = c(67L, 97L, 85L, 67L, 52L, 72L, 
        55L, 62L, 99L, 89L, 65L, 58L, 77L, 88L), sampled = c(44L, 
        67L, 77L, 52L, 41L, 66L, 49L, 35L, 17L, 66L, 48L, 48L, 22L, 
        65L)), class = "data.frame", row.names = c(NA, -14L))
    

    You were very close with mapply() but you made it harder than it needs to be:

    df$sampled <- mapply(function(x, y) sample(seq(x, y), 1), df$start, df$end)
    df
    #    ID start end sampled
    # 1   a    25  67      67
    # 2   b    36  97      86
    # 3   c    23  85      54
    # 4   d    15  67      36
    # 5   e    21  52      37
    # 6   f    43  72      60
    # 7   g    39  55      44
    # 8   h    27  62      37
    # 9   i    11  99      86
    # 10  j    21  89      52
    # 11  k    28  65      65
    # 12  l    44  58      51
    # 13  m    16  77      62
    # 14  n    25  88      31
    
    0 讨论(0)
  • 2021-01-27 05:56

    Figured it out. df[j,4] <- mapply(function(x, y) sample(seq(x, y), 1), df[j,"start"], df[j,"end"])

    I just needed to be specific as to which row of the sampled values I wanted to enter into df[j,4]. Specifying row j for columns start and end did the trick.

    0 讨论(0)
提交回复
热议问题