I am a C++ programmer and I am new to R. Somebody told me that using a for loop in R is a bad idea and that it is better to use sapply
. I wrote the following code to calculate the probability of a birthday coincidence:
prob <- 1 # prob of no coincidence
days <- 365
k <- 50 # how many people
probability <- numeric() #probability vector (empty right now)
for(i in 1:k){
prob <- (days - i + 1)/days * prob # Formula for no coincidence
probability[i] <- 1 - prob
}
How I can do the same thing with sapply
? I want to do something like:
1 - sapply(1:length(m), function(x) prod(m[1:x]))
But how to use the formula for no coincidence of birthday?
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.)
For you specific question it's probably best to just use the built-in birthday probability calculator
sapply(1:50, pbirthday)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With