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

  • Home
  • SEARCH
  • 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 9208253
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T00:34:36+00:00 2026-06-18T00:34:36+00:00

In other R code, it is common to see data.frame declared before a loop

  • 0

In other R code, it is common to see data.frame declared before a loop is started.
Suppose I have data frame data1 with 2000 rows.
And in a loop, I am via web service looping over data1 to create a new data.frame data2. (Please don’t recommend not using a loop).
And in data2$result and data2$pubcount I need to store different values for each of the 2000 data1 items.

Do I HAVE to declare before the loop

data2=data.frame()

and do I have to tell R how many rows and what columns I will later use? I know that columns can be added without declaring. What about rows. Is there advantage in doing:

data2<-data.frame(id=data1$id)

I would like to do only what I absolutely HAVE to declare and do.
Why the empty declaration gives error once in the loop?

later edit: Speed and memory is not of issue. 10s vs. 30s makes no difference and I have a under 100MB data and big PC (8GB). Matrix is not an option since the data is numbers and text (mixed), so I have to use non-matrix.

  • 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-06-18T00:34:37+00:00Added an answer on June 18, 2026 at 12:34 am

    Something like this:

    df <- data.frame(a=numeric(n),b=character(n))
    
    for (i in 1:n) {
    
    #<do stuff>
    df[i,1] <- ...
    df[i,2] <- ...
    
    }
    

    You should avoid manipulation of data.frames in a loop, since subsetting of data.frames is a slow operation:

    a <- numeric(n)   
    b <- character(n) 
    
    for (i in 1:n) {
    
    #<do stuff>
    a[i] <- ...
    b[i] <- ...
    
    }
    df <- data.frame(a,b)
    

    Of course, there are often better ways than a for loop. But it is strongly recommended to avoid growing objects (and I wont teach you how to do that). Pre-allocate as shown here.

    Why should you pre-allocate? Because growing objects in a loop is sloooowwwww and that’s one of the main reasons why people think loops in R are slow.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

it has a property: string Code and 10 other. common codes is list of
I'm using code from other c# app and I can see that in WP7
A lot of C++ code uses syntactical conventions for marking up data members. Common
Can I combine code that uses System.Drawing.Bitmap together with other code in a program
When I'm writing my DAL or other code that returns a set of items,
Where to find Generic.xaml (or other code with the default look) for native WPF
Is there a framework or other sample code for doing barcode recognition (create and
I am trying to test some other Python code repeatedly, using all possible combinations
I am wondering if a conditional operator could actually prevent other unrelated code from
The other day this code was working. I changed some things and re-ran it

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.