Reputation: 35
I am trying to expand a dataframe by including, for each row, 500 simulated values from a Poisson distribution whose parameter Theta (count_mean) is already stored in the dataframe. In the example below I am only providing a dataframe example, since my real data is composed by more than 50,000 rows (i.e. ids).
example.data <- data.frame(id=c("4008", "4118", "5330"),
count_mean=c(2, 25, 11)
)
So for each row, I know I have to generate the simulated values by:
rpois(500, example.data$count_mean)
How can I introduce these values into the same dataframe, in which each new column presents one simulated value for each row?
Upvotes: 0
Views: 307
Reputation: 16998
Another option using dplyr
and tidyr
:
example.data %>%
rowwise() %>%
mutate(poisson = list(rpois(500, count_mean))) %>%
unnest(poisson) %>%
group_by(id) %>%
mutate(count=row_number()) %>%
pivot_wider(names_from="count", names_prefix="sim_", values_from="poisson")
Upvotes: 0
Reputation: 11981
You can use sapply
to simulate the numbers and then use cbind
to bind your data together:
simdata <- t(sapply(example.data$count_mean, function(x) rpois(500, x)))
colnames(simdata) <- paste0("sim_", 1:500)
cbind(example.data, simdata)
However, I would encourage you to work with a different data format: maybe a long table would be more appropriate in this situation than the current wide table.
Upvotes: 1