Here are multiple ways how to rename columns in R using base functionality or dplyr functions like rename and select.
Here is my data frame for the multiple scenarios of changing data frame column names.
df <- data.frame( Values1 = sample(1:10, 5, replace=T), Values2 = sample(1:10, 5, replace=T), Values3 = sample(1:10, 5, replace=T) )
Rename the R data frame column by location
By using names or colnames functions, you can see the column index number and use that to rename the necessary one.
#change name of the first column names(df)[1] <- 'NewName'
It is fast and simple, but be aware – if the column changes position that might lead to an error.
Rename the R data frame column by name
Here is the simplest method.
names(df)[names(df) == 'Values1'] <- 'NewName'
Of course, there are plenty of other ways to do that. Use the R function that can confirm the match. Like grepl or match functions.
names(df)[match(names(df), 'Values1')] <- 'NewName'
Rename multiple R data frame columns at the same time
First of all, you should create a set of column names for all of the data frame columns and then do the replacement.
NewNames <- c('NewName1', 'NewName2', 'NewName3') names(df) <- NewNames
If the column changes position that might lead to an error.
Rename R data frame columns with dplyr
While there is a special function rename in the right situation, you can change the names of the columns in one take with the select function.
require(dplyr) iris %>% select( SL = Sepal.Length, SW = Sepal.Width, PL = Petal.Length, PW = Petal.Width, Species ) %>% head() # SL SW PL PW Species #1 5.1 3.5 1.4 0.2 setosa #2 4.9 3.0 1.4 0.2 setosa #3 4.7 3.2 1.3 0.2 setosa #4 4.6 3.1 1.5 0.2 setosa #5 5.0 3.6 1.4 0.2 setosa #6 5.4 3.9 1.7 0.4 setosa
That is one of my top 10 favorite dplyr tips and tricks.
The function rename from dplyr
You just have to point out the new column name and the old one. One of the advantages is that if you press Tab inside rename function, you can get a necessary column name and avoid typos.
That will be useful when you cannot integrate this process inside selecting columns.
require(dplyr) #rename only one column df <- df %>% rename(NewName = Values1) #rename all columns df <- df %>% rename(NewName = Values1, NewName2 = Values2, NewName3 = Values3)
Sometimes it is not necessary to rename columns in R. For example if you want to use the join functions from dplyr, but data frames have different names for key columns. Look at this post to lear more about that.
If you want to know useful RStudio tips and tricks, take a look at this post.
Leave a Reply