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
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.
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
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.