Krina M Krina M - 1 year ago 108
R Question

For Loop for Iterations and Store Result in Dataframe

I am trying to learn for loop. Below is the code. I want to simulate log normal distribution as shown below with sample size 200, and want to replicate (different results, but similar distribution with similar mean and SD) it 100 times. How do I store results in a dataframe or matrix? By below mentioned code, I am only getting results for 1st replicate (200 subjects).

Any tips will be very helpful.


nRep <- 100
for(i in 1:nRep) {
cv.tumor= 0.40
Base_Tumor <- exp(rnorm(n, log(8.5), sd.tumor))

Answer Source

Maybe you're only seeing the first 200 but the code returns all the info separated by list (with each list having 200 elements).

Anyway, with you can get a dataframe with all the results together.