N. J.
N. J.

Reputation: 105

R - Simulating data with loop

I simulated data (1000 rows) following an exponential function with a mean of 120

  sim_data <- rexp(1000, 1/120)

How do I repeat this over 100 days to get the simulated data in a data frame like so:

head(sim_data_frame)

   sim_data1    sim_data2    sim_data3 ... sim_data100


 [1] 59.33708159  29.23829096  247.02014989  71.85972065
 [2] 14.03171085  46.64195945  38.03259199   92.18882163
...
 [1000]109.83320146 90.00037210   7.29312409   2.67249848 

Upvotes: 1

Views: 74

Answers (2)

akrun
akrun

Reputation: 887951

We can use replicate to do this n times

do.call(cbind, replicate(4, rexpr(1000, 1/120), simplify = FALSE))

Or use

replicate(4, rexpr(1000, 1/120))

Upvotes: 3

ThomasIsCoding
ThomasIsCoding

Reputation: 102890

You can also try matrix

n <- 100
matrix(rexp(1000*n, 1/120),ncol = n)

Upvotes: 1

Related Questions