I am trying to select some specific columns within a data frame using select statement by passing the header names into a variable so as to reduce writing the columns names.
The data frame I am working with is:
structure(list(`Row Labels` = c("X1", "X2", "X3"), `2022-11-01` = c(1,
2, 3), `2022-12-01` = c(2, 3, 4), `2023-01-01` = c(2, 2, 3),
`2023-02-01` = c(3, 3, 4), `2023-03-01` = c(3, 2, 3), `2023-04-01` = c(4,
3, 4), `2023-05-01` = c(4, 2, 3), `2023-06-01` = c(5, 3,
4), `2023-07-01` = c(5, 2, 3)), class = c("tbl_df", "tbl",
"data.frame"), row.names = c(NA, -3L))
The code which I wrote is:
Book1 <- read_excel("C:/X/X/X/Book1.xlsx")
View(Book1)
Time <- '2022-08-01'
Time_Period <- add_months(max(ymd(Time)),1:3)
Time_Period
Book1 %>%
select(`Row Labels`,any_of(Time_Period))
I run into an error in this select statement and not been able to figure out why. Can anyone help me out here. The duplicate which was suggested doesn't work in this scenario.