0

I'm trying to loop through several data frames that I import and convert all the variables to lowercase. I currently do it this way (I have about 20 data frames in my actual code):

datasets <- c("iris", "mtcars")

for (i in 1:length(datasets)) {
  df <- as.data.frame(get(datasets[i]))
  colnames(df) <- tolower(colnames(df))
  assign(paste0(datasets[i]), df)
}

I've previously read that using get and assign are not best practice so I was wondering how I could do this differently.

ANam
  • 337
  • 2
  • 15
  • 3
    Perhaps `list2env(lapply(mget(datasets), \(x) setNames(x, tolower(names(x))), .GlobalEnv)` (recommend to keep it in a `list` instead of creating multiple objects in the global env) – akrun Sep 08 '22 at 16:41
  • Related: https://stackoverflow.com/q/17559390/6574038 – jay.sf Sep 08 '22 at 16:44

0 Answers0