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

Ask A Question

Stats

  • Questions 510k
  • Answers 510k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You are comparing a float to a double. the literal… May 16, 2026 at 4:51 pm
  • Editorial Team
    Editorial Team added an answer table img { display: block; margin: 0 auto; } May 16, 2026 at 4:50 pm
  • Editorial Team
    Editorial Team added an answer You can record sound (PCM samples) from the microphone to… May 16, 2026 at 4:50 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I am in a situation where I would like to implement Ajax Push in
I would like to implement the Ramer–Douglas–Peucker_algorithm in C++. The pseudo code looks like
We would like to implement slightly different behavior in our web application for those
I would like to implement a basic versioning system in a MySQL table. Let's
I want to program a chess engine which learns to make good moves and
I have a Java program that runs many small simulations. It runs a genetic
I'm creating a linux program in C++ for a portable device in order to
Whenever I see a problem that would be shared by others, with a solution
I'm working on a small simulation that is running on my 8-core workstation. The
I'm making a game which you can see here, if you are on Windows

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.