Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sapply in R, how to use?

Tags:

r

sapply

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?

like image 216
Edwardo Avatar asked Dec 26 '22 22:12

Edwardo


2 Answers

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

like image 100
flodel Avatar answered Jan 02 '23 07:01

flodel


For you specific question it's probably best to just use the built-in birthday probability calculator

sapply(1:50, pbirthday)
like image 27
Dason Avatar answered Jan 02 '23 06:01

Dason