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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T06:53:15+00:00 2026-06-07T06:53:15+00:00

I have a dataframe running into about 500,000 rows. One of these columns contains

  • 0

I have a dataframe running into about 500,000 rows. One of these columns contains positive integer values, say column A. let there be another column B

I now need to create a second dataframe with number of rows equal to sum(dataframe$A). this is done.

A question of performance arises when i need to fill this new data frame up with data. I am trying to create a column A2 for this second frame as follows:

A2<-vector() 
for (i in 1:nrow(dataframe)){
  A2<-c(A2,rep(dataframe$B[i],dataframe$A[i]))
}

The external loop is obviously very slow for the large number of rows being processed. Any suggestions on how to achieve this task with faster processing.

Thanks for responses

  • 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-07T06:53:16+00:00Added an answer on June 7, 2026 at 6:53 am

    You simply do not need the loop at all. rep is already vectorized.

    A2 <- rep(dataframe$B, dataframe$A)
    

    Should work. As a reproducible example, here is your way using the built in mtcars dataset.

    x <- vector()
    for(i in 1:nrow(mtcars)) {x <- c(x, rep(mtcars$cyl[i], mtcars$gear[i]))}
    > x
      [1] 6 6 6 6 6 6 6 6 4 4 4 4 6 6 6 8 8 8 6 6 6 8 8 8 4 4 4 4 4 4 4 4 6 6 6 6 6
     [38] 6 6 6 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 8
     [75] 8 8 8 8 8 8 8 8 8 8 8 4 4 4 4 4 4 4 4 4 4 4 4 4 4 8 8 8 8 8 6 6 6 6 6 8 8
    [112] 8 8 8 4 4 4 4
    

    and vectorized, it is:

    x2 <- rep(mtcars$cyl, mtcars$gear)
    > x2
      [1] 6 6 6 6 6 6 6 6 4 4 4 4 6 6 6 8 8 8 6 6 6 8 8 8 4 4 4 4 4 4 4 4 6 6 6 6 6
     [38] 6 6 6 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 8
     [75] 8 8 8 8 8 8 8 8 8 8 8 4 4 4 4 4 4 4 4 4 4 4 4 4 4 8 8 8 8 8 6 6 6 6 6 8 8
    [112] 8 8 8 4 4 4 4
    

    which will be orders of magnitude faster than using a loop.

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

Sidebar

Related Questions

I have a dataframe where one of the columns contains a set of names.
I have a DataFrame with a few columns. One columns contains a symbol for
I have a dataframe (14.5K rows by 15 columns) containing billing data from 2001
I have a dataframe with one column that contains several hundreds of dates in
I have a data frame in R that has come about from running some
I have a dataframe x with this values: x1 x2 x3 1 NA 4
I have a dataframe with some numeric columns. Some row has a 0 value
I have a dataframe with over 200 columns. The issue is as they were
I have a dataframe with numeric entries like this one test <- data.frame(x =
I have a quite large data frame, about 10 millions of rows. It has

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.