By now, if you are like me, you are probably getting annoyed at writing “flowers.m3” or “species.richness” everytime you need to refer to one of those columns. This will happen ALOT with data you enter or obtain from others, as R converts any spaces to fullstops and people tend to capitilise most words. The
rename() function allows us to simply rename a column name within our data frame.
Personally, this is my favourite function in R as I hate captials, fullstops and other annoying column name problems that slow down coding or generate errors. Trust me, when you spend an hour trying to fix a line of code only to find a single capital letter is missing, you will understand.
To do this with dplyr (a tidyverse package) we simply use the following command:
weeds <- rename(weeds, flowers = flowers.m3) # In the brackets we need to specify our data frame (weeds) followed by a second argument specifying the name we want for our column = the name we already have. # Again, if you run this by itself it will not save to your data frame, unless you direct it to your data frame variable using the <-
Pretty simple and straightforward.
If you want to rename multiple columns, this is a pretty simple addition. For demonstrating purposes, I am going to rename all the columns of weeds to nonsensical crap.
grocerylist <- rename(weeds, coopers = "flowers", asahi = "species", vb = "soil", littlecreatures = "weeds") # to specify multiple variables/columns, we simply add a comma after the first rename and keep going.