In this article, we’d like to show you how to rename data frame columns in R by using R base functions or other libraries such as dplyr, plyr, which is often needed when manipulating our data.
1. Preparation
We will prepare a data frame so that we can practice renaming its columns in the below sections. Here is the code to create a simple data frame of books.
1 2 3 4 5 |
names <- c('The Great Gatsby','The Grapes Of Wrath','Nineteen Eighty Fou','Ulysses') bys <- c('Scott Fitzgerald', 'John Steinbeck', 'George Orwell', 'James Joyce') points <- c(994, 963, 946, 925) # create a data frame df <- data.frame(names, bys, points) |
Let’s see the data frame
1 2 3 4 5 6 |
> head(df) a 1 The Great Gatsby Scott Fitzgerald 994 2 The Grapes Of Wrath John Steinbeck 963 3 Nineteen Eighty Fou George Orwell 946 4 Ulysses James Joyce 925 |
1. Rename column of data frame with R base functions
In this sections, we will rename column of a data frame by using R base functions which don’t require us to install any other libraries or packages.
1.1. Use the colnames() function
The colnames function is used to retrieve or set the column names.
1.1.1. Change or rename all columns
1 |
colnames (df) <- c("Title", "Author", "Points") |
1.1.2. Change or rename a column
We can rename the column by specifying the index of the column we want to change in the column names vector.
1 |
colnames(df)[1] <- "Title" |
or specify exactly the column name we want to change or rename:
1 2 |
colnames(df)[colnames(df)=="bookNames"] <- "Title" colnames(df)[colnames(df)=="bys"] <- "Author" |
or
1 2 |
colnames(df)[which(colnames(df) == "bookNames")] <- "Title" colnames(df)[which(colnames(df) == "bys")] <- "Author" |
1.2. Use the names() function
We can use the names() function which is similar to the colnames() function, to rename column of a data frame.
1.2.1. Rename all columns
1 |
names(df) <- c("Title", "Author", "Points") |
1.1.2. Rename a specific column
We can change the column name by specifying the index of the column we want to rename in the column names vector.
1 |
names(df)[1] <- "Title" |
or specify exactly the column name we want to rename:
1 2 |
names(df)[names(df)=="bookNames"] <- "Title" names(df)[names(df)=="bys"] <- "Author" |
or
1 2 |
names(df)[which(names(df) == "bookNames")] <- "Title" names(df)[which(names(df) == "bys")] <- "Author" |
2. Rename a data frame column with the dplyr package
Another R package enables us to rename the column of data frame in R is the dplyr package. Let’s get started by install and load the package
2.1. Install and load the dplyr package
1 |
install.packages("dplyr") |
1 |
library(dplyr) |
2.2. Use the rename() function to change or rename the column
For example, to change the “bookNames” column to “Title”
1 |
df <- rename(df, Title = bookNames) |
And to change the columns “bookNames” to “Title and “bys” to “Author”
1 |
df <- rename(df, Title = bookNames, Author = bys) |
2.3. Use the select() function
We can use the select function to rename the column of a data frame. Just note that this function only keeps only variables we specify in the arguments.
For example, we will change the columns “bookNames” to “Title and “bys” to “Author”.
1 |
newdf <- select(df, Title = bookNames, Author = bys) |
Note that the newdf data frame only remains 2 columns Title and Author.
3. Rename column of data frame with the plyr package
In similar to the dplyr, we can use the plyr package to change the column names of a data frame in R.
3.1. Install and load the plyr package
1 |
install.packages("plyr") |
1 |
library(plyr) |
3.2. Use the rename() function to rename the columns
For example, we will change the names of columns “bookNames” to “Title and “bys” to “Author” by using the rename function:
1 |
df <- rename(df, c("bookNames" = "Title", "bys" = "Author")) |
or
1 |
df <- rename(df, replace = c("bookNames" = "Title", "bys" = "Author")) |
4. Summary
We have seen several ways to rename data frame columns in R. The first way is to use the base R functions which doesn’t require us to install any additional packages. The second way is to use other R packages such as plyr, dplyr, etc. These packages provide us a shorter way to rename data frame columns.
Below are other posts related to R data frame. You can refer to them if you’re interested in.
R – Add New Column To A Data Frame
R Data Frame and basic functions
R – Add New Column To A Data Frame