Reputation: 801
I want to apply this code on several lists of dataframes (df).
df1<- lapply(df1, function(x) {
x$Date <- as.Date((x$Date), format="%Y-%m-%d")
x})
The dataframe lists are called df1
, df2
, df3
, df4
and abc1
, abc2
, abc3
, abc4
. The Date-column is always on the same place.
I tried this to get the df1-4
done, but it doesn't work.
for (i in 1:4) {
df[i] <- lapply(df[i], function(x) {
x$Date <- as.Date((x$Date), format="%Y-%m-%d")
x})}
I also thought about getting all the filenames into a list and looping with that:
df_list = c("df1","df2", "df3", "df4", "abc1", "abc2", "abc3", "abc4")
But I haven't succeeded with that. I want to keep the original names of the files. Any suggestions?
Upvotes: 0
Views: 38
Reputation: 2867
for(i in 1:length(df_list)) {
df_list[[i]] <- lapply(df_list[[i]], function(x) {
x$Date <- as.Date((x$Date), format="%Y-%m-%d")
x})
}
Does this work for you?
For me it does:
class(df_list[[1]][[1]]$Date)
[1] "Date"
Your error seemed to happen because you used []
instead of [[]]
. You have to use double brackets to refer to the data.frame.
Upvotes: 1