Hello,
I am glad you got to a solution. I would not recommend this solution you're following though. Rather make use of the split s_year
column to also do a filter check by year. See the new updated version based on your new data below:
library(tidyverse)
library(lubridate)
#>
#> Attaching package: 'lubridate'
#> The following objects are masked from 'package:base':
#>
#> date, intersect, setdiff, union
df <- data.frame(
month = c("2020 Jan",
"2020 Feb","2020 Mar","2020 Apr","2020 May","2020 Jun",
"2020 Jul","2020 Aug","2020 Sep","2020 Oct","2020 Nov",
"2020 Dec","2021 Jan","2021 Feb","2021 Mar","2021 Apr",
"2021 May","2021 Jun","2021 Jul","2021 Aug","2021 Sep",
"2021 Oct","2021 Nov","2021 Dec"),
sales = c(1337158,1024617,969186,580039,
745976,588006,594815,549379,1277256,980282,853458,1007229,
1246084,1193005,1503203,1451290,1343771,1582470,
1233360,1271090,1337158,1024617,969186,580039)
)
# Splitting your variable into year month
df_output <-
df %>% separate(month, c("s_year", "s_month"), sep = " ", remove = FALSE) %>%
mutate(s_year = as.numeric(s_year))
# changing your abreviated month into a number
df_output <- df_output %>% mutate(s_month = s_month %>% unlist() %>% match(month.abb))
# creating our filter variables
filter_month <- Sys.time() %>% lubridate::month() - 1
filter_year <- Sys.time() %>% lubridate::year()
# filter the data to the currect subset
df_filter <- df_output %>% filter(s_month <= filter_month & s_year == filter_year) %>% select(-c(s_month,s_year))
df_filter
#> month sales
#> 1 2020 Jan 1337158
#> 2 2020 Feb 1024617
#> 3 2020 Mar 969186
#> 4 2020 Apr 580039
#> 5 2020 May 745976
#> 6 2020 Jun 588006
#> 7 2020 Jul 594815
#> 8 2020 Aug 549379
#> 9 2020 Sep 1277256
Created on 2020-10-10 by the reprex package (v0.3.0)