Let's say I have the following function
get_answer <- function(condition, dp, rp){
if(condition == "DD"){
result <- rbinom(n = 2, size = 1, prob = dp)
}
if(condition %in% c("DR", "RD")){
result <- c(rbinom(n = 1, size = 1, prob = dp),
rbinom(n = 1, size = 1, prob = rp))
}
if(condition == "RR"){
result <- rbinom(n = 2, size = 1, prob = rp)
}
return(result)
}
I create a data.frame like so:
results_df <- data.frame(condition = c(rep("DD", 10000), rep("DR", 10000), rep("RR", 10000)))
I want to be able to get the vector returned from get_answer
, for the condition in the column condition
, and split the returned values into two columns -- the first value going into column P1
and the second going into column P2
.
Something like this:
results_df %>% mutate(p1 = get_answer(condition, .6, .4)[0], p2 = get_answer(condition, .6, .4)[1])
What's the correct way to do this in dplyr?