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?
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.