Tuesday, June 25, 2013

all variable names to lower case - automatically!

here is the code in two pieces.  I like to do it in two steps instead of one because I tend to use the vector with the variable names for other things:

var.names<-tolower(colnames(data)
colnames(data)<-var.names


This is assuming your dataset is called "data"

Essentially, it takes the column names (e.g. variable names), puts them into a vector called var.names, then you assign that vector to the column names of the dataset "data".

The only issue I have run into is if you have two variables with the same name, but one in upper case and one in lower case in the same dataset.  In this case, it will delete one of them.  In this case, you are best to rename this duplicate variable first... or better yet, figure out why the heck you have two variable names with the same name in the same dataset!

Take that!


3 comments:

  1. yessss this showed up as one of the first google results for this problem, saved me tons of times thank you

    ReplyDelete
    Replies
    1. Great! Glad to hear it. Good luck with your work.

      If you need to change the case of data in the columns/rows, you can do this same thing.
      lowercasecolumn<-tolower(datasetname$uppercasecolumn)

      Delete
  2. I think you are missing one )

    var.names<-tolower(colnames(data))

    Thank you, though.

    ReplyDelete