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 284707
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T05:28:52+00:00 2026-05-12T05:28:52+00:00

Suppose I want perform a simulation using the following function : fn1 <- function(N)

  • 0

Suppose I want perform a simulation using the following function:

fn1 <- function(N) {
  res <- c()
  for (i in 1:N) {
    x <- rnorm(2)
    res <- c(res, x[2]-x[1])
  }
  res
}

For very large N, computation appears to hang. Are there better ways of doing this?

(Inspired by: https://stat.ethz.ch/pipermail/r-help/2008-February/155591.html)

  • 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-12T05:28:53+00:00Added an answer on May 12, 2026 at 5:28 am

    For loops in R are notoriously slow, but here there’s another issue. It’s much faster to preallocate the results vector, res, rather append to res at each iteration.

    Below we can compare the speed of the above version with a version that simply starts with a vector, res, of length N and changes the ith element during the loop.

    fn1 <- function(N) {
      res <- c()
      for (i in 1:N) {
         x <- rnorm(2)
         res <- c(res,x[2]-x[1])
      }
      res
    }
    fn2 <- function(N) {
      res <- rep(0,N)
      for (i in 1:N) {
         x <- rnorm(2)
         res[i] <- x[2]-x[1]
      }
      res
    }
    > N <- 50000
    > system.time(res1 <- fn1(N))
       user  system elapsed 
      6.568   0.256   6.826 
    > system.time(res2 <- fn2(N))
       user  system elapsed 
      0.452   0.004   0.496 
    

    Also, as Sharpie points out, we can make this slightly faster by using R functions like apply (or its relatives, sapply and lapply).

    fn3 <- function(N) {
      sapply( 1:N, function( i ){ x <- rnorm(2); return( x[2] - x[1] ) } )
    }
    > system.time(res3 <- fn3(N))
       user  system elapsed 
      0.397   0.004   0.397 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

A) suppose a table. that i want to perform a DELETE function on .
I need a method to perform the following task, suppose if I have a
In Javascript, suppose I want to perform some cleanup when an exception happens, but
Suppose I have two tables, which both have user ids. I want to perform
so suppose I detect a user's ip using some code to perform restrictions.... is
Let's suppose I have seven tables, and I want to perform the same query
What I'm trying to accomplish is the following: Suppose I have a function that
Suppose there is CheckBox and I want to perform checkBox_CheckedChanged event when it is
Suppose I want to add two buffers and store the result. Both buffers are
Suppose I want to store N samples (each sample takes up a significant portion

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.