title: Simulation with Tidyverse -- putting data into tibble format
body: <p>I am trying to run simulations in R using the tidyverse. This code works, but doesn't scale well to more than a few variables. </p>
  
  <p>Any thoughts on how to improve this? I've tried <code>purrr</code> but I didn't find any success.</p>
  
  <p>The example below draws 5 values from a normal distribution and repeats this 3 times. How could I repeat it <code>n</code> times instead of 3?</p>
  
  <pre><code>n = 5
  
  x=1:n
  y1 = rnorm(n)
  y2 = rnorm(n)
  y3 = rnorm(n)
  
  # put data into tibble
  df &lt;- tibble(x=x, y1=y1, y2=y2, y3=y3)
  
  # Tidy data -- go from wide to long
  df &lt;- pivot_longer(df, cols=starts_with('y'))
  
  # Make plot
  ggplot(df, aes(x=x, y=value, group=name, color=name))+
    geom_line()
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/xx6M9.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/xx6M9.png" alt="output"></a></p>