I want to generate a random number u from 1 to 4 for each line in my data.frame. However, the probabilities are different for each line and these probabilities are stored in the variables p00, p10, p01, p11. This means that the probability for 1 is p00, for 2 p01 etc.. I have 406611 lines in my data.frame. data01 contains the variables p00, p10, p01, p11.
I am doing the following
data02 <- data01 %>%
mutate (u = sample(x=c(1:4), size=406611, replace = T, prob=c(p00, p10, p01, p11))
However I get the following error:
Error in sample.int(length(x), size, replace, prob) :
incorrect number of probabilities.
If I do this (just to try) I do not get any error:
data02 <- data01 %>%
mutate (u = sample(x=c(1:4), size=406611, replace = T, prob=c(0.25, 0.25, 0.25, 0.25))
Why is this not working? How can I generate the numbers?
Aucun commentaire:
Enregistrer un commentaire