I have a data frame called data like as shown below
structure(list(NRIC_ID = c(1234L, 4567L, 1234L, 3578L, 2468L),
ADMIT_DATE = structure(c(2L, 3L, 4L, 5L, 1L), .Label = c("11/11/2011",
"2/12/2016", "3/11/2019", "5/7/2018", "7/7/2014"), class = "factor"),
test_date = structure(c(3L, 5L, 2L, 1L, 4L), .Label = c(" 2014-17-11",
"10/8/2013", "11/2/2012", "12/12/2012", "12/2/2014"), class = "factor"),
test2_DATE = structure(c(2L, 3L, 4L, 5L, 1L), .Label = c("11/11/2011",
"2/12/2016", "3/11/2019", "5/7/2018", "7/7/2014"), class = "factor")), class = "data.frame", row.names = c(NA,
-5L))
Currently when I get the datatypes of the columns using the below code
(sapply(data, class))
I get the output as Integer, Date, and character.
Now, I would like to identify all the columns that contain DATE or date in their column name using pattern matching and convert them to the Date data type
I tried the below code for pattern matching
data %>%
select(contains("date")) %>% #how to ignore case here
as.Date()
When I do this, I get the below error
Error in as.Date.default: do not know how to convert '.' to class "Date"
can help me to fix this?