Search code examples
rrowsrepeat

Loop over DataFrame Rows to build another DataFrame


Hi I've never used loops before and I do not even know what it's possible to achieve with them and if they are necessary to perform this task.

I have a dataframe where there are 2 columns, ValueDate and MaturityDate, I need to create a DataFrame where each day of the year is a different report where ReportDate is >= ValueDate & < MaturityDate. BDebt is the original Data Base with all the rows. ReportDate is a vector with a range of dates from min(BDebt) date to today().

DataBase:

HSBC 500 1-Jan 5-Jan
JPMO 750 2-Jan 4-Jan
CITI 230 3-Jan 4-Jan

Output Expected:

1-Jan HSBC 500 1-Jan 5-Jan
2-Jan HSBC 500 1-Jan 5-Jan
2-Jan JPMO 750 2-Jan 4-Jan 
3-Jan HSBC 500 1-Jan 5-Jan
3-Jan JPMO 750 2-Jan 4-Jan
3-Jan CITI 230 3-Jan 4-Jan
4-Jan HSBC 500 1-Jan 5-Jan

Solution

  • With data.table:

    library(data.table)
    
    BDebt <- data.table(
      ID = c("HSBC", "JPMO", "CITI"),
      val = c(500, 750, 230),
      start_date = seq(as.Date("2022/1/1"), as.Date("2022/1/3"), "days"),
      end_date = c(as.Date("2022/1/5"), as.Date("2022/1/4"), as.Date("2022/1/4"))
    )
    
    ReportDate <- setorder(
      BDebt[
        rep.int(
          1:nrow(BDebt),
          ndays <<- end_date - start_date
        )
      ][
        , at_date := start_date + sequence(ndays, 0)
      ][
        , c(5, 1:4)
      ],
      at_date
    )
    ReportDate
    #>       at_date   ID val start_date   end_date
    #> 1: 2022-01-01 HSBC 500 2022-01-01 2022-01-05
    #> 2: 2022-01-02 HSBC 500 2022-01-01 2022-01-05
    #> 3: 2022-01-02 JPMO 750 2022-01-02 2022-01-04
    #> 4: 2022-01-03 HSBC 500 2022-01-01 2022-01-05
    #> 5: 2022-01-03 JPMO 750 2022-01-02 2022-01-04
    #> 6: 2022-01-03 CITI 230 2022-01-03 2022-01-04
    #> 7: 2022-01-04 HSBC 500 2022-01-01 2022-01-05