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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T20:36:07+00:00 2026-06-10T20:36:07+00:00

I have a data set of the form: df <- data.frame(var1 = c(1976-07-04 ,

  • 0

I have a data set of the form:

df <- data.frame(var1 = c("1976-07-04" , "1980-07-04" , "1984-07-04" ), 
                   var2 = c('d', 'e', 'f'), 
                   freq = 1:3)

I can expand this data.frame very quickly using indexing by:

df.expanded <- df[rep(seq_len(nrow(df)), df$freq), ]

I however want to have create a sequence instead of a replicate on the date and have the freq tell me the length of the this. i.e for row 3 i can create the entries to fill the exploded data.frame with:

seq(as.Date('1984-7-4'), by = 'days', length = 3)

Can anyone suggest a fast method for doing this? My method is to use various lapply functions to do this

I used a combination of Gavin Simpson’s answer and a previous idea for my solution.

ExtendedSeq <- function(df, freq.col, date.col, period = 'month') {
  #' An R function to take a data fame that has a frequency col and explode the 
  #' the dataframe to have that number of rows and based on a sequence.
  #'  Args:
  #'   df: A data.frame to be exploded.
  #'   freq.col: A column variable indicating the number of replicates in the 
  #'             new dataset to make.
  #'   date.col: A column variable indicating the name or position of the date
  #'             variable.
  #'   period: The periodicity to apply to the date.

  # Replicate expanded data form
  df.expanded <- df[rep(seq_len(nrow(df)), df[[freq.col]]), ]

  DateExpand <- function(row, df.ex, freq, col.date, period) {
    #' An inner functions to explode a data set and build out days sequence
    #'  Args:
    #'    row: Each row of a data set 
    #'    df.ex: A data.frame, to expand
    #'    freq: Column indicating the number of replicates to make.
    #'    date: Column indicating the date variable
    #'  Output:
    #'    An exploded data set based on a sequence expansion of a date.
    times <- df.ex[row, freq]
    # period <- can edit in the future if row / data driven.
    date.ex <- seq(df.ex[row, col.date], by = "days", length = times)
    return(date.ex)
  }

dates <- lapply(seq_len(nrow(df)), 
                FUN = DateExpand, 
                df.ex = df,
                freq = freq.col,
                col.date = date.col,
                period = period)

df.expanded[[date.col]] <- as.Date(unlist(dates), origin = '1970-01-01')
row.names(df.expanded) <- NULL
return(df.expanded)
}

Personally i dont like the way i need to covert the dates back from the list and supply an origin based on this conversion in case this changes in teh future, but i really appreciate the ideas and 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-06-10T20:36:09+00:00Added an answer on June 10, 2026 at 8:36 pm

    Here is one way:

    extendDF <- function(x) {
        foo <- function(i, z) {
            times <- z[i, "freq"]
            out <- data.frame(seq(z[i, 1], by = "days", length = times),
                              rep(z[i, 2], times),
                              rep(z[i, 3], times))
            names(out) <- names(z)
            out
        }
        out <- lapply(seq_len(nrow(x)), FUN = foo, z = x)
        do.call("rbind", out)
    }
    

    This iterates over the indices 1:nrow(df) (i.e. the row indices of df) applying the in-line function foo to each row of df. foo() essentially just extends var2 and freq a freq number of times and uses your seq() call for extending var1. The function makes some assumptions about the column orderings, names etc but you can modify that should you wish.

    The only other bit is that it is far more efficient to convert var1 to a "Date" object all in one rather than for each row in turn in extendDF(), hence first do a single conversion, here using transform():

    df <- transform(df, var1 = as.Date(var1))
    

    then call extendDF()

    extendDF(df)
    

    This gives:

    R> df <- transform(df, var1 = as.Date(var1))
    R> extendDF(df)
            var1 var2 freq
    1 1976-07-04    d    1
    2 1980-07-04    e    2
    3 1980-07-05    e    2
    4 1984-07-04    f    3
    5 1984-07-05    f    3
    6 1984-07-06    f    3
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Another ggplot legend question! I have a dataset of the form test <- data.frame(
I have the swiss data set provided by R, which has the following form:
I have a data set that resembles this: id product_id size color price created_date
I have a data frame of the form: Family Code Length Type 1 A
I have a data set in key/value form: setlist = {{'135350258120342034': {'title':'The Matrix'} {'235350258120342034':
I currently have a form set up that passes some data to a php
I have a data set (~10000 rows) with the following form: +---------------------------+---------------+-------------+ | DateTimeCreated
I have a customer with a very small set of data and records that
I got this problem. I have a form that retrieves a table data using
I have a question, how to read a specific column form data set.... I

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.