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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T17:34:19+00:00 2026-06-14T17:34:19+00:00

I am having a challenge with a piece of code that takes very long

  • 0

I am having a challenge with a piece of code that takes very long to execute and I am wondering what are the key tricks to optimize the execution time of this code. I have to admit that the input data.frame is significant (140,000 rows) and that the output data.frame is approximately 220,000 rows.

A sample of the input data.frame:

head(extremes)
X_BusinessIDDescription     min         max         month
ID105                       2007-12-01  2008-06-01  2007-12-01
ID206                       2007-12-01  2009-07-01  2007-12-01
ID204                       2007-12-01  2008-02-01  2007-12-01
ID785                       2008-07-01  2010-08-01  2008-07-01
ID125                       2007-11-01  2008-07-01  2007-11-01
ID107                       2007-11-01  2011-06-01  2007-11-01

The data.frame that will be extended with the loop. The data.frame is initiated to get the structure in place.

output <- extremes[1,]
output
X_BusinessIDDescription     min         max         month
ID105                       2007-12-01  2008-06-01  2007-12-01

Other values

IDcounter <- 1
IDmax <- nrow(extremes)
linecounter <- 1

The while-loop I would like to optimize:

while (IDcounter <= IDmax){
    start <- extremes$min[IDcounter]
    end <- extremes$max[IDcounter] # add three months
    while(start <= end){
        output[linecounter,] <- extremes[IDcounter,]
        output$month[linecounter] <- start
        linecounter <- linecounter+1
        start <- seq(start, by ="month", length=2)[2]
    }
    IDcounter <- IDcounter + 1
}

For a small number of rows this code executes pretty quickly, but it seems like it is slowing down as the output extends.

The output looks something like this:

head(output)
X_BusinessIDDescription     min         max         month
ID105                       2007-12-01  2008-06-01  2007-12-01
ID105                       2007-12-01  2008-06-01  2008-01-01
ID105                       2007-12-01  2008-06-01  2008-02-01
ID105                       2007-12-01  2008-06-01  2008-03-01
ID105                       2007-12-01  2008-06-01  2008-04-01
ID105                       2007-12-01  2008-06-01  2008-05-01

For every month in the interval between min and max in the extreme file is an row created.

I also would be interested to learn how I can can that this code can take ready of the multiple cores of computing resources available. OK, I admit this is not really an optimization but it will reduce the execution time, which is important as well.

Jochem

  • 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-14T17:34:20+00:00Added an answer on June 14, 2026 at 5:34 pm

    As @CarlWitthoft already mentioned you have to rethink your data structure because of many duplicated data.

    Here you find a simple vectorized approach:

      ## create all possible ranges of months
      ranges <- mapply(function(mi, ma) {seq(from=mi, to=ma, by="month")}, mi=extremes$min, ma=extremes$max)
    
      ## how many months per ID?
      n <- unlist(lapply(ranges, length))
    
      ## create new data.frame
      output <- data.frame(X_BusinessIDDescription=rep(extremes$X_BusinessIDDescription, n),
                          min=rep(extremes$min, n),
                          max=rep(extremes$max, n),
                          month=as.Date(unlist(ranges), origin="1970-01-01"), stringsAsFactors=FALSE)
    

    Comparison to your approach:

    extremes <- data.frame(X_BusinessIDDescription=c("ID105", "ID206", "ID204", "ID785", "ID125", "ID107"),
                          min=as.Date(c("2007-12-01", "2007-12-01", "2007-12-01", "2008-07-01", "2007-11-01", "2007-11-01")),
                          max=as.Date(c("2008-06-01", "2009-07-01", "2008-02-01", "2010-08-01", "2008-07-01", "2011-06-01")),
                          month=as.Date(c("2007-12-01", "2007-12-01", "2007-12-01", "2008-07-01", "2007-11-01", "2007-11-01")),
                          stringsAsFactors=FALSE)
    
    approachWhile <- function(extremes) {
      output <- data.frame(X_BusinessIDDescription=NA, min=as.Date("1970-01-01"), max=as.Date("1970-01-01"), month=as.Date("1970-01-01"), stringsAsFactors=FALSE)
      IDcounter <- 1
      IDmax <- nrow(extremes)
      linecounter <- 1
      while (IDcounter <= IDmax){
        start <- extremes$min[IDcounter]
        end <- extremes$max[IDcounter] # add three months
        while(start <= end){
            output[linecounter,] <- extremes[IDcounter,]
            output$month[linecounter] <- start
            linecounter <- linecounter+1
            start <- seq(start, by ="month", length=2)[2]
        }
        IDcounter <- IDcounter + 1
      }
      return(output)
    }
    
    approachMapply <- function(extremes) {                       
      ranges <- mapply(function(mi, ma) {seq(from=mi, to=ma, by="month")}, mi=extremes$min, ma=extremes$max)
    
      n <- unlist(lapply(ranges, length))
    
      output <- data.frame(X_BusinessIDDescription=rep(extremes$X_BusinessIDDescription, n),
                          min=rep(extremes$min, n),
                          max=rep(extremes$max, n),
                          month=as.Date(unlist(ranges), origin="1970-01-01"), stringsAsFactors=FALSE)
      return(output)
    }
    
    identical(approachWhile(extremes), approachMapply(extremes)) ## TRUE
    
    library("rbenchmark")
    
    benchmark(approachWhile(extremes), approachMapply(extremes), order="relative")
    #                      test replications elapsed relative user.self sys.self
    #2 approachMapply(extremes)          100   0.176     1.00     0.172    0.000
    #1  approachWhile(extremes)          100   6.102    34.67     6.077    0.008
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im having very tough challenge in converting the following code C#, can anybody help
LIVE CODE: http://jsfiddle.net/vy4nY/ I'm following this challenge , but I'm having some issues. I'm
I'm having some problems with authentication in iOS programming. I have a code that
Having trouble with each function... Will try to explain by example... In my code,
Having a hard time with labels on a ggplot2 plot. Here's a similar plot
Having trouble accessing javascript code in a mixed html/js ajax response. jQuery ajax doc
You have some code you want to remove associated with an obsolete piece of
I'm having quite a challenge creating an appropriate rewrite rule for Apache/2.2.14 on Fedora
We've having trouble deploying a web service that works in our development environment, but
I'm having quite a challenge... I have a web page where the user clicks

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.