1

is there a better way to change the class of these dataframes and columns than repeating this? Thanks!

 df46$`Measurement Date` <- as.Date(df46$`Measurement Date`, format = "%Y-%m-%d")
 df45$`Measurement Date` <- as.Date(df45$`Measurement Date`, format = "%Y-%m-%d")
 df44$`Measurement Date` <- as.Date(df44$`Measurement Date`, format = "%Y-%m-%d")
 df43$`Measurement Date` <- as.Date(df43$`Measurement Date`, format = "%Y-%m-%d")
 df42$`Measurement Date` <- as.Date(df42$`Measurement Date`, format = "%Y-%m-%d")
 df41$`Measurement Date` <- as.Date(df41$`Measurement Date`, format = "%Y-%m-%d")
 df40$`Measurement Date` <- as.Date(df40$`Measurement Date`, format = "%Y-%m-%d")

Would using the lapply function make more sense?

 lapply(df46:df40[`Measurement Date`], Date)
Starbucks
  • 1,448
  • 3
  • 21
  • 49
  • If the data.frames are of similar structure, consider putting them into a list. [This post](http://stackoverflow.com/questions/17499013/how-do-i-make-a-list-of-data-frames) would be worth a read. gregor's answer there gives a number of tips on working with lists of data.frames. My answer provides a fairly simple way to get similarly names data.frames into a list. – lmo Mar 27 '17 at 19:22
  • you could use lapply to do the job. But you would get a list of data.frames. They might be difficult to work with. May I ask why do you have that many data.frames in the first place? – DJJ Mar 27 '17 at 19:23
  • @DJJ Each data frame is approx 250MB files and there are about 25 files. R is not letting me change the combined dataframe classes for some reason, so I have to scrub each file individually before combining. – Starbucks Mar 27 '17 at 19:28

1 Answers1

4

You can try with eval parse:

for (i in 40:46){
  eval(parse(text =
  paste0('df',i,'$`Measurement Date` <-
         as.Date(df',i,'$`Measurement Date`, format = "%Y-%m-%d")')
  ))
}
Nick Kennedy
  • 12,510
  • 2
  • 30
  • 52
Vasilis Vasileiou
  • 507
  • 2
  • 8
  • 20
  • 2
    Nice answer. Possibly more readable would be to use `eval(substitute())` e.g. ``eval(substitute(df$`Measurement Date` <- as.Date(df$`Measurement Date`, format = "%Y-%m-%d"), list(df = as.name(paste0("df", i)))))`` – Nick Kennedy Mar 27 '17 at 19:40