0

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.

enter image description here

user20203146
  • 447
  • 7
  • Not sure where `add_months` comes from, but you want to make sure you coerce the date to a string, and then use the `any_of()` tidyselect helper. – MrFlick Nov 09 '22 at 14:46
  • I have tried it as well, it didn't work for some reason. Couldn't figure out why? I have attached the screenshot. – user20203146 Nov 09 '22 at 15:50
  • 1
    Does this work for you: `Book1 %>% select(\`Row Labels\`, any_of("2023-05-01"))` What exactly is the content of `Time_Period`? It's hard to tell from an image – MrFlick Nov 09 '22 at 16:00
  • @MrFlick Time_Period = "2022-09-01" "2022-10-01" "2022-11-01" , that would work but when I pass it through the variable, it doesn't work. And since the aim is to get the code in such a manner that I don't have to mentioned the column names as the number of columns are very long in the actual dataset I am working on. I was trying to figure out a way around it. – user20203146 Nov 09 '22 at 16:22
  • 1
    I'm not sure I understand. This should work: `Time_Period = c("2022-11-01","2023-01-01","2023-07-01"); Book1 %>% select(\`Row Labels\`, any_of(Time_Period))`. (here I used column names that actually exist in the sample data) – MrFlick Nov 09 '22 at 16:44
  • It should have right. That is why I was racking my brain around. I just realized the Time_Period <- add_months(max(ymd(Time)),1:3) returns data in date format and if I convert it to character Time_Period <-as.character(add_months(max(ymd(Time)),1:freq)), it ideally should work. Let me check. – user20203146 Nov 09 '22 at 17:16
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/249454/discussion-between-user20203146-and-mrflick). – user20203146 Nov 09 '22 at 17:16

0 Answers0