How do I select all column indexes that contain the date from another data frame? I am trying to select all column indexes from dataset_rep
that are contained in dates_data$dates
. The below code isn't working. Only index 1 is listed, but I expected the other returns to not be NULL
. I would appreciate help and if you could explain why this isn't working that would be great! I'm trying to keep the output the same format as it is now but accurately have the expected indexes. Something is wrong with the loop I think.
I expected not just 1
but 1,3,4,5 indexes.
Reproducible example:
library(tidyverse)
library(lubridate)
dataset_rep <-
data.frame(`2022-01-01_adoptions` = c(122, 122,133),
`2021-12-11_adoptions` = c(111,111,133),
`2022-03-08_adoptions` = c(444,222,111),
`2022-04-01_adoptions` = c(444,389,122),
`2022-04-11_adoptions` = c(333,542,123),
`2022-05-01_adoptions` = c(421,555,211))
dataset_rep <-
dataset_rep %>%
rename(`2022-01-01_adoptions` = `X2022.01.01_adoptions`)
dataset_rep <-
dataset_rep %>%
rename(`2021-12-01_adoptions` = `X2021.12.11_adoptions`)
dataset_rep <-
dataset_rep %>%
rename(`2022-03-08_adoptions` = `X2022.03.08_adoptions`)
dataset_rep <-
dataset_rep %>%
rename(`2022-04-01_adoptions` = `X2022.04.01_adoptions`)
dataset_rep <-
dataset_rep %>%
rename(`2022-04-11_adoptions` = `X2022.04.11_adoptions`)
dataset_rep <-
dataset_rep %>%
rename(`2022-05-01_adoptions` = `X2022.05.01_adoptions`)
dates_data <-
data.frame(dates = c('2022-01-01', '2022-03-08', '2022-04-01', '2022-04-11'))
cols_relevant <- vector("list", nrow(dates_data))
for (i in seq_along(dates_data)) {
cols_relevant[[i]] <- grep(dates_data$dates[[i]], colnames(dataset_rep))
} #1,3,4,5 indexes would have been expected
Thank you