I am trying to build a simple simulation of data that takes in a unique mean and standard devation per row (My input dataset (data
in the code below) is 100+ rows consisting of Name
, mean_proj
, and proj_std
. I want to simulate the range of outcomes for each row many times over to then perform analysis on. The following is what I have done:
n_sims <- 5000
sim_results <- data.frame(matrix(NA, nrow = as.numeric(count(data)), ncol = 0))
for (i in 1:as.numeric(count(data))) {
sim_results[i, "name"] <- data$Name[i]
for (j in 1:n_sims) {
sim_results[i, paste0("sim_", j)] <- qnorm(runif(1), data$mean_proj[i], data$proj_std[i])
}
}
The good news is that it works (creates a new dataframe called sim_results
with a name
column followed by many columns sim_n
with that iteration of the simulated data), the bad news, is that as n_sims
gets larger, it takes a LOT longer to run. Is there a better, more r-centric, way that I should be looking in to? Would love some tips!