Hi All,
Is there a way to do similar thing solved in previous post here
Replacing Values based on previous Years value (Revisiting) - tidyverse - Posit Community (rstudio.com)
But this time for previous month's value instead of previous years. Lag function doesn't seem to help when we have NA values. This code works perfectly for obtaining previous year's value. But looking to have an approach to fill data with previous month's value including NAs to be filled with newly received previous months data with or without lag function.
library(tidyverse)
library(lubridate)
vol <- data.frame(
Date = c("2018 Jan","2018 Jan","2018 Jan","2018 Jan",
"2018 Feb","2018 Feb","2018 Feb","2018 Feb",
"2018 Mar","2018 Mar","2018 Mar","2018 Mar",
"2018 Apr","2018 Apr","2018 Apr","2018 Apr",
"2018 May","2018 May","2018 May","2018 May",
"2018 Jun","2018 Jun","2018 Jun","2018 Jun",
"2018 Jul","2018 Jul","2018 Jul","2018 Jul",
"2018 Aug","2018 Aug","2018 Aug","2018 Aug",
"2018 Sep","2018 Sep","2018 Sep","2018 Sep",
"2018 Oct","2018 Oct","2018 Oct","2018 Oct",
"2018 Nov","2018 Nov","2018 Nov","2018 Nov",
"2018 Dec","2018 Dec","2018 Dec","2018 Dec",
"2019 Jan","2019 Jan","2019 Jan","2019 Jan",
"2019 Feb","2019 Feb","2019 Feb","2019 Feb",
"2019 Mar","2019 Mar","2019 Mar","2019 Mar",
"2019 Apr","2019 Apr","2019 Apr","2019 Apr",
"2019 May","2019 May","2019 May","2019 May",
"2019 Jun","2019 Jun","2019 Jun","2019 Jun",
"2019 Jul","2019 Jul","2019 Jul","2019 Jul",
"2019 Aug","2019 Aug","2019 Aug","2019 Aug",
"2019 Sep","2019 Sep","2019 Sep","2019 Sep",
"2019 Oct","2019 Oct","2019 Oct","2019 Oct",
"2019 Nov","2019 Nov","2019 Nov","2019 Nov",
"2019 Dec","2019 Dec","2019 Dec","2019 Dec",
"2020 Jan","2020 Jan","2020 Jan","2020 Jan",
"2020 Feb","2020 Feb","2020 Feb","2020 Feb",
"2020 Mar","2020 Mar","2020 Mar","2020 Mar",
"2020 Apr","2020 Apr","2020 Apr","2020 Apr",
"2020 May","2020 May","2020 May","2020 May",
"2020 Jun","2020 Jun","2020 Jun","2020 Jun",
"2020 Jul","2020 Jul","2020 Jul","2020 Jul",
"2020 Aug","2020 Aug","2020 Aug","2020 Aug",
"2020 Sep","2020 Sep","2020 Sep","2020 Sep",
"2020 Oct","2020 Oct","2020 Oct","2020 Oct",
"2020 Nov","2020 Nov","2020 Nov","2020 Nov",
"2020 Dec", "2020 Dec","2020 Dec", "2020 Dec",
"2021 Jan","2021 Jan","2021 Jan","2021 Jan",
"2021 Feb","2021 Feb","2021 Feb","2021 Feb",
"2021 Mar","2021 Mar","2021 Mar","2021 Mar",
"2021 Apr","2021 Apr","2021 Apr","2021 Apr",
"2021 May","2021 May","2021 May","2021 May",
"2021 Jun","2021 Jun","2021 Jun","2021 Jun",
"2021 Jul","2021 Jul","2021 Jul","2021 Jul",
"2021 Aug","2021 Aug","2021 Aug","2021 Aug",
"2021 Sep","2021 Sep","2021 Sep","2021 Sep",
"2021 Oct","2021 Oct","2021 Oct","2021 Oct",
"2021 Nov","2021 Nov","2021 Nov","2021 Nov",
"2021 Dec","2021 Dec", "2021 Dec","2021 Dec"),
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",
"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",
"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",
"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"),
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",
"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",
"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",
"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"
),
Sales = c(100,110, 120, 130, 140, 150, 160, 170, 180, 190, 200, 210,100,110, 120, 130, 140, 150, 160, 170, 180, 190, 200, 210,100,110, 120, 130, 140, 150, 160, 170, 180, 190, 200, 210,100,110, 120, 130, 140, 150, 160, 170, 180, 190, 200, 210,
220, 230,240, 250, 260, 270, 280, 290, 300, 310, 320, 330,220, 230,240, 250, 260, 270, 280, 290, 300, 310, 320, 330,20, 230,240, 250, 260, 270, 280, 290, 300, 310, 320, 330,220, 230,240, 250, 260, 270, 280, 290, 300, 310, 320, 330,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA)
)
vol$Date <- ym(vol$Date)
# Works for Previous Year's Value (Solved in another Post)
# Looking to modify it for Previous Month's Value
(result <- mutate(vol,
rn = row_number()
) |>
arrange(Country, Type, lubridate::month(Date)) |>
group_by(Country, Type, lubridate::month(Date)) |>
tidyr::fill(Sales, .direction = "down") |>
ungroup() |>
arrange(rn) |>
select(-last_col(), -rn))