Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sapply() is an avoidable complication in code block 2.1 #6

Open
dave-lovell opened this issue Mar 20, 2023 · 0 comments
Open

sapply() is an avoidable complication in code block 2.1 #6

dave-lovell opened this issue Mar 20, 2023 · 0 comments

Comments

@dave-lovell
Copy link

I've just been watching lecture 2, and when R example 2.1 comes up (35:00), you explain that

For those of you who don't use R, sapply is just a loop, it's just a function that loops over a list

Because R vectorises functions by default, you don't need to use sapply here. This might make the code flow clearer, since you wouldn't need to explain the purpose of sapply.

I've written a small counter-example that uses vectorisation rather than sapply:

sample <- c("W", "L", "W", "W", "W", "L", "W", "L", "W")
W <- sum(sample == "W")
L <- sum(sample == "L")
p <- c(0, 0.25, 0.5, 0.75, 1)

get_ways <- function(q) (q*4)^W * ((1-q)*4)^L
ways <- get_ways(p)

prob <- ways/sum(ways)

cbind(p, ways, prob)
#>         p ways       prob
#> [1,] 0.00    0 0.00000000
#> [2,] 0.25   27 0.02129338
#> [3,] 0.50  512 0.40378549
#> [4,] 0.75  729 0.57492114
#> [5,] 1.00    0 0.00000000

Created on 2023-03-20 with reprex v2.0.2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant