Yo yo yo.
Ive been using this code snippet a lot lately. Most of our databases have variable names that are mixed upper and lower case letters. As you may have guessed by now, Im not a huge fan of upper case - its hard to remember which variables have which case of text.
So.
After you read in your dataset (since you are a master after the "how to read in your data" post)... paste the following code into the console or the R script editor. Once you paste it all, highlight the two lines and hit command+enter (on the mac!) to run it.
test<-tolower(colnames(data))
colnames(data)<-test
What is happening here is as follows:
The first line takes the column names (your variable names!) from the dataset called data (if you named your data something else, you need to change this). The colnames function is built into the base R package, so you dont need any particular package to make this work. The tolower function is also built into the base R package. So this function takes the column names from your dataset, data, and puts everything to lowercase text. you can also use "toupper" if you prefer all upper case text. You may notice that on the far left (before the gets "<-" operator), i have the word "test". What happens here is that R takes all of the lowercase names and puts it into a vector out on its own. You use this in the next line.
The second line takes the variable names that are all lowercase in the "test" vector and applies them to the column names of the dataset, data. Again, if your dataset is named something other than data, you need to change this.
Are you starting to see the utility of always keeping your dataset named "data"? I hope so. It is pretty handy.
This is a little more advanced than I was hoping to get at this point, but it is such a useful function - and I pretty much use it every time I import a dataset, I figured it would be useful to stick this at the beginning.
This blog post wasn't funny you say? No humor? Yeah, you are right. It was a long day, so I have no humor left. I have two humerus's, but I don't think that is what you were hoping for.
No comments:
Post a Comment