You could do:
m <- (days - seq_len(k) + 1) / days
probability <- 1 - sapply(seq_along(m), function(x) prod(m[1:x]))
but that would be missing on the useful cumprod
function:
probability <- 1 - cumprod(m)
which will be a lot faster.
(Also gave you a peak at seq_along
and seq_len
which are more robust than :
when dealing with zero-length vectors.)