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)
3
  • If the data.frames are of similar structure, consider putting them into a list. This post 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. Commented Mar 27, 2017 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? Commented Mar 27, 2017 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. Commented Mar 27, 2017 at 19:28

1 Answer 1

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")')
  ))
}
Sign up to request clarification or add additional context in comments.

1 Comment

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)))))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.