I'm having some issues with a quite basic issue. I tried to find any threads who is having the same issue but couldn't find any.
I'm trying to figure out how to generate a Bernoulli variable (y) which is based on probabilities (z) I have generated for each observation. I've generated the fictive dataset below to represent my problem.
x <- c("A", "B", "C", "D", "E", "F")
z <- c(0.11, 0.23, 0.25, 0.06, 0.1, 0.032)
df <- data.frame(x, z)
I want to add the variable y which is a binary variable based upon the probabilities from variable z.
I tried the following:
df <- df %>%
mutate(y = rbinom(1,1,z))
But it seems like it gives the same value to all observation, and not based on the observation's own probability.
Does anyone know how to solve this?
Thanks!
From the online documentation for
rbinom
:So
To demonstrate that events are generated with the correct probabilities: