Transforming as.Date: multiple data frames at once

617 views Asked by At

I have 66 data frames that I need to transform the "Date" column "as.Date". I dont know how to do it at once. I'm going like this:

dat2003q1$Date<-as.Date(as.character(dat2003q1),format="%m/%d/%Y")
dat2003q2$Date<-as.Date(as.character(dat2003q2),format="%m/%d/%Y")
dat2003q3$Date<-as.Date(as.character(dat2003q3),format="%m/%d/%Y")

And so on...

There is a way to do it for all data frames at once? Ive found a solution for multiple columns in the same data frame, but not like this.

Tks

1

There are 1 answers

4
akrun On BEST ANSWER

Try

lst <- lapply(mget(ls(pattern='^dat2003q\\d+')), function(x) {
                     x$Date <- as.Date(as.character(x$Date), format='%m/%d/%Y')
                       x})

If you want to update the datasets in the global environment with this change (which is not that recommended as you can do all the necessary operations within the list and later you may save the datasets using read.table)

list2env(lst, envir=.GlobalEnv)