I have multiple dataframes (df1, df2, df3, df4, df5), and each dataframe has the same two columns: date, which has dates (e.g. 2020-11-12) as characters and price column as numeric. For example, df1 looks like this: df1
| Date | Price |
|---|---|
| 2020-11-12 | 29.75 |
| 2020-11-13 | 29.95 |
| 2020-11-14 | 30.72 |
| 2020-11-15 | 32.83 |
| 2020-11-16 | 33.14 |
I am trying to use lapply with a custom function that converts the character "date" column to the date class. However, lapply function doesn't give me a reformatted date column. My simple codes are as follows:
df.list <- list(df1, df2, df3, df4, df5) # create a list of dataframes
# create a custom function to change the class of date column
date_con <- function(x) {
x$date <- as.Date(x$date, format="%Y-%m-%d")}
When I use
lapply(df.list, date_con)
the date columns still remain as character. For instance, when I check class(df$date), it still shows as "character" but not date. On the other hand, if I go manually for each dataframe to make this conversion, it works but I don't want to go over 100s of dataframes manually. That is,
df1$date <- as.Date(df1$date, format="%Y-%m-%d")
works but it is obviously not efficient and I am sure there is a way to achieve this. So, how can I use lapply or some other approach to efficiently convert character date column into a date class column for large number of dataframes?