library(tidyverse)
nsource <- 5
mydata <- structure(list(p1 = c(0.153603238035774, 0.251732841184133),
p2 = c(0.0108935803858148, 0.12041117307058),
p3 = c(0.712158280629968, 0.257556345715588),
p4 = c(0.0529741150781822, 0.17690785400165),
p5 = c(0.0703707858702605, 0.193391786028049)),
row.names = 1:2, class = "data.frame")
mydata <- mydata %>%
rowwise %>%
mutate(P = sample(1:nsource, size = 1, replace = TRUE,
prob = c_across(matches('^p\\d+$')))) %>%
ungroup
> Error: `c_across()` must only be used inside dplyr verbs.
I have a dataset mydata
p1 p2 p3 p4 p5
1 0.1536032 0.01089358 0.7121583 0.05297412 0.07037079
2 0.2517328 0.12041117 0.2575563 0.17690785 0.19339179
where each row contains 5 probabilities that sum up to one. I want to sample a number called "P" from 1:5 w/ probability p1:p5. However, running the above code gives me the error that "c_across() must only be used inside dplyr verbs."