Skip to content
Home ยป Changing all data frame column names to upper case in R

Changing all data frame column names to upper case in R

One of the compulsive things I do in my programming life is trying to keep all column names in uppercase. Inbetween my code you can often find a line of code that manually adjusts the column names of a data frame. However, I read the following in a colleague’s code today, and I thought I’d share it.

The following function loops over all the objects in an environment. Then it checks if it is a data frame (or data.table). If so, it changes the column names of the data frames to uppercase.

up <- function() {
  for (df in ls(.GlobalEnv)[1:(length(ls(.GlobalEnv))-1)]){
    e <<- .GlobalEnv
    if (is.data.frame(e[[df]])) {
      colnames(e[[df]]) <<- toupper(colnames(e[[df]]))
    }
  }
  rm(e,pos=".GlobalEnv")
}

Now you can simply run the following four characters of code after every chunk of code to guarantee all your data frame column names are properly cased.

up()

By the way, if you’re having trouble understanding some of the code and concepts, I can highly recommend “An Introduction to Statistical Learning: with Applications in R”, which is the must-have data science bible. If you simply need an introduction into R, and less into the Data Science part, I can absolutely recommend this book by Richard Cotton. Hope it helps!

Good luck!

Say thanks, ask questions or give feedback

Technologies get updated, syntax changes and honestly… I make mistakes too. If something is incorrect, incomplete or doesn’t work, let me know in the comments below and help thousands of visitors.

4 thoughts on “Changing all data frame column names to upper case in R”

  1. Thank You. It really worked.
    I also have another question. What if I have more than one dataframe and I want to change all the column names of all the Dataframes to upper case, using one code to convert all??
    If you can post, will be a great help..!

    Thanks in Advance.

    Regards,
    Gyandeep

    1. Hi Gyandeep

      Thank you for your reply. I’m not sure if I understand your question, because this is exactly what the up() function does. It changes the column names of all the columns of ALL the data frames to uppercase.

      Could you be more specific?
      Thanks
      Roel

Leave a Reply

Your email address will not be published. Required fields are marked *