Search code examples
rloopsglobal-variables

looping through the global environment and rename the dataframes to lowercase with no special characters (in R)


I've looked into this, and this discussions before asking the following question: In my global environment, I have datasets, lists, and other vectors. I would like to remove all special characters and apply a tolower() function globally to these data frames.

Therefore, a dataset named TTxxSSS will be ttxxsss. A dataset named çã will be ca.

-I imagine I'll have to loop through the environment objects and rename only the datasets.
-A solution that I can generalize to other tasks will be amazing (i.e., trim the names, etc). That's the code:

ds1_01_HaPpy <- data.frame(x=rep(10,10))
df_02_PÇaPp6 <- data.frame(x=rep(10,10))
df_ZzZzzzzZZ <- data.frame(x=rep(10,10))
x <- c(12,3)
a <- list("a","b" ,"c")

Thank you


Solution

  • Loop through the names and for each data frame transliterate it and convert to lower case. If that results in a name that differs from the original name and the new name does not already exist then by default issue a message and rename it.

    There are arguments that can be used to turn off the messages (verbose), allow existing objects to be overwritten (overwrite), allow processing of names that begin with dot (all) and to test it out without making any changes (test).

    clean_dfs <- function(envir = .GlobalEnv, all = FALSE, 
        overwrite = FALSE, test = FALSE, verbose = TRUE) {
      for(nm in ls(all = all, envir)) {
        .tmp <- get(nm, envir)
        if (is.data.frame(.tmp)) {
          newnm <- tolower(iconv(nm, to = "ASCII//TRANSLIT"))
          if (nm != newnm) {
            if (exists("newnm", envir) && !overwrite) {
              warning(newnm, " already exists -- ", nm, " not renamed")
            } else {
              if (verbose) cat("renaming", nm, "to", newnm, "\n")
              if (!test) {
                assign(newnm, .tmp, envir)
                rm(list = nm, envir = envir)
              } 
            }
          }
        }
      }
    }
    

    Test it out and then run it.

    # test without making any changes
    clean_dfs(test = TRUE)
    ## renaming df_02_PÇaPp6 to df_02_pcapp6 
    ## renaming df_ZzZzzzzZZ to df_zzzzzzzzz 
    ## renaming ds1_01_HaPpy to ds1_01_happy 
    
    ls() # unchanged
    ## [1] "a"            "clean_dfs"    "df_02_PÇaPp6" "df_ZzZzzzzZZ" "ds1_01_HaPpy"
    ## [6] "x"    
    
    # now run it for real
    clean_dfs()
    ## renaming df_02_PÇaPp6 to df_02_pcapp6 
    ## renaming df_ZzZzzzzZZ to df_zzzzzzzzz 
    ## renaming ds1_01_HaPpy to ds1_01_happy 
    
    ls()
    ## [1] "a"            "clean_dfs"    "df_02_pcapp6" "df_zzzzzzzzz" "ds1_01_happy"
    ## [6] "x"