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.)
对于您的具体问题,最好只使用内置的生日概率计算器
sapply(1:50, pbirthday)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.