You can do:
m <- (days - seq_len(k) + 1) / days probability <- 1 - sapply(seq_along(m), function(x) prod(m[1:x]))
but this will be absent in the useful cumprod function:
probability <- 1 - cumprod(m)
which will be much faster.
(Also gave you a peak in seq_along and seq_len , which is more stable than : when working with vectors of zero length.)
source share