data <- data.frame(p1 = runif(10, 0, 1), p2 = runif(10, 0, 1), p3 = runif(10, 0, 1))
I want to sample from (1, 2, 3) based on probabilities (p1, p2, p3). Currently, I've written this in a loop
for(i in 1:nrow(data)){
data$x <- sample(c(1, 2, 3), size = 1, replace = TRUE, prob = c(data$p1[i], data$p2[i], data$p3[i])
}
Is there a more efficient way to do this? This is quite slow as the number of rows in the data set increases.