I want to write a function that will accept a vector of columns that are of class character, that will convert them to date, and output the dataframe. When I run it, it outputs a list of dataframes but without the conversion taking place. Can I rewrite the below to achieve this? The context is that I might have lots of character columns that I want to convert and having a function seems the best way to achieve this.
library(tidyverse)
date1<-c("2021-01-05","2021-01-06","2021-01-07","2021-01-08","2021-01-09")
date2<-c("2021-02-05","2021-02-06","2021-02-07","2021-02-08","2021-02-09")
df<-data.frame(date1,date2,stringsAsFactors = FALSE)
#the vector of columns to convert to date format
datecols<-c("date1","date2")
datefunc<-function(a){
df2<-df%>%
mutate(a=as.Date(a,"%Y-%m-%d" ))
return(df2)}
df<-lapply(datecols,datefunc)