Function to extend data with date

Hi All,

Is there a function that can be used to extend data with date. For example, if data has monthly values till today and I want to fill in the numbers for same data but for future 1 year in months, is there a function that can extend the data?

Thanks!

I think your question is about forecasting

Anyway, heres a book

Thanks @nirgrahamuk !
In this case, we are not forecasting, but we are just trying to add some figures manually for next few months based on growth%. So, for following sample data, I am looking to extend the data for 2024 Year by month with empty Sales at the moment which we will fill manually.

df <- 
data.frame(
  stringsAsFactors = FALSE,
              Date = c("2023-01-01","2023-01-01",
                       "2023-01-01","2023-01-01","2023-02-01","2023-02-01",
                       "2023-02-01","2023-02-01","2023-03-01","2023-03-01",
                       "2023-03-01","2023-03-01","2023-04-01","2023-04-01",
                       "2023-04-01","2023-04-01","2023-05-01","2023-05-01",
                       "2023-05-01","2023-05-01","2023-06-01","2023-06-01",
                       "2023-06-01","2023-06-01","2023-07-01","2023-07-01",
                       "2023-07-01","2023-07-01","2023-08-01","2023-08-01",
                       "2023-08-01","2023-08-01","2023-09-01","2023-09-01",
                       "2023-09-01","2023-09-01","2023-10-01","2023-10-01","2023-10-01",
                       "2023-10-01","2023-11-01","2023-11-01","2023-11-01",
                       "2023-11-01","2023-12-01","2023-12-01","2023-12-01",
                       "2023-12-01","2023-12-01"),
           Country = c("CA","CA","US","US","CA",
                       "CA","US","US","CA","CA","US","US","CA","CA","US",
                       "US","CA","CA","US","US","CA","CA","US","US",
                       "CA","CA","US","US","CA","CA","US","US","CA","CA",
                       "US","US","CA","CA","US","US","CA","CA","US",
                       "US","CA","CA","US","US","US"),
              Type = c("A","B","A","B","A","B",
                       "A","B","A","B","A","B","A","B","A","B","A","B",
                       "A","B","A","B","A","B","A","B","A","B","A",
                       "B","A","B","A","B","A","B","A","B","A","B",
                       "A","B","A","B","A","B","A","B","B"),
             Sales = c(460,470,480,490,500,510,
                       520,530,540,550,560,570,460,470,480,490,500,510,
                       520,530,540,550,560,570,60,470,480,490,500,
                       510,520,530,540,550,560,570,460,470,480,490,500,
                       510,520,530,540,550,560,570,-2)
)

Do you mean something like this:

library(dplyr)
library(lubridate)

df <- data.frame("date"= as.Date(c("2023-09-01", "2022-08-30", "2021-07-15")))

df <- df %>%
  mutate(plus_year = date %m+% months (12))

df %>% flextable::flextable()

image

library(tidyverse)

(new_part <- expand_grid(
  Date = seq.Date(
    from = as.Date("2024-01-01"),
    by = "1 month",
    length.out = 12
  ),
  distinct(df, Country, Type), Sales = NA
))


altogether <- bind_rows(
  df |> mutate(Date = as.Date(Date)),
  new_part
)

Well! Actually, I was looking to extend data by rows. So, after 2023-12-01, I was looking for 2024-01-01, 2024, 02-01 and so on until 2024-12-01

Thanks @nirgrahamuk !
Yes, this is it!
Thanks a bunch!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.