Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1043701
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T15:42:37+00:00 2026-05-16T15:42:37+00:00

I would like to implement a simulation program, which requires the following structure: It

  • 0

I would like to implement a simulation program, which requires the following structure:

It has a for loop, the program will generate an vector in each iteration. I need each generated vector is appended to the existing vector.

I do not how how to do this in R. Thanks for the help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T15:42:39+00:00Added an answer on May 16, 2026 at 3:42 pm

    These answers work, but they all require a call to a non-deterministic function like sample() in the loop. This is not loop-invariant code (it is random each time), but it can still be moved out of the for loop. The trick is to use the n argument and generate all the random numbers you need beforehand (if your problem allows this; some may not, but many do). Now you make one call rather than n calls, which matters if your n is large. Here is a quick example random walk (but many problems can be phrased this way). Also, full disclosure: I haven’t had any coffee today, so please point out if you see an error 🙂

    steps <- 30
    n <- 100
    directions <- c(-1, 1)
    
    results <- vector('list', n)
    
    for (i in seq_len(n)) {
      walk <- numeric(steps)
      for (s in seq_len(steps)) {
        walk[s] <- sample(directions, 1)
      }
      results[[i]] <- sum(walk)
    }
    

    We can rewrite this with one call to sample():

    all.steps <- sample(directions, n*steps, replace=TRUE)
    dim(all.steps) <- c(n, steps)
    walks <- apply(all.steps, 1, sum)
    

    Proof of speed increase (n=10000):

    > system.time({
    + for (i in seq_len(n)) {
    +   walk <- numeric(steps)
    +   for (s in seq_len(steps)) {
    +     walk[s] <- sample(directions, 1)
    +   }
    +   results[[i]] <- sum(walk)
    + }})
       user  system elapsed 
      4.231   0.332   4.758 
    
    > system.time({
    + all.steps <- sample(directions, n*steps, replace=TRUE)
    + dim(all.steps) <- c(n, steps)
    + walks <- apply(all.steps, 1, sum)
    + })
       user  system elapsed 
      0.010   0.001   0.012 
    

    If your simulation needs just one random variable per simulation function call, use sapply(), or better yet the multicore package’s mclapply(). Revolution Analytics’s foreach package may be of use here too. Also, JD Long has a great presentation and post about simulating stuff in R on Hadoop via Amazon’s EMR here (I can’t find the video, but I’m sure someone will know).

    Take home points:

    • Preallocate with numeric(n) or vector('list', n)
    • Push invariant code out of for loops. Cleverly push stochastic functions out of code with their n argument.
    • Try hard for sapply() or lapply(), or better yet mclapply.
    • Don’t use x <- c(x, rnorm(100)). Every time you do this, a member of R-core kills a puppy.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to implement a distinct thread for each route in apache camel.I
I would like to implement a tool that generates graphs whose memory will be
I would like to implement a very simple way to store a variable containing
I would like to implement a function with R that removes repeated characters in
I would like to implement a simple queueing service specific to a project. Where
I would like to implement a switch button, android.widget.Switch (available from API v.14). <Switch
I would like to implement a simple AR desktop application. This application should first
I would like to implement something similar to 37Signals's Yellow Fade effect. I am
We would like to implement a web form that automatically saves content at regular
I would like to implement some Drag-select functionality into a project of mine but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.