How to Rename a Column Name in R | 3 Examples to Change Colnames of a Data Frame
Basic R Syntax:
# Change colname of one column colnames(data)[colnames(data) == "Old_Name"] <- "New_Name" # Change colnames of all columns colnames(data) <- c("New_Name1", "New_Name2", "New_Name3") # Change colnames of some columns colnames(data)[colnames(data) %in% c("Old_Name1", "Old_Name2")] <- c("New_Name1", "New_Name2")
As R user you will agree: To rename column names is one of the most often applied data manipulations in R. However, depending on your specific data situation, a different R syntax might be needed.
Do you need to change only one column name in R? Would you like to rename all columns of your data frame? Or do you want to replace some variable names of your data, but keep the other columns like they are?
Above, you can find the basic R code for these three data situations. For further illustration, I’m going to show you in the following tutorial how to rename a column in R, based on 3 reproducible examples.
Let’s dive in…
Example 1: Rename One Column Name in R
For the following examples, I’m going to use the iris data set. Let’s have a look how the data looks like:
data(iris) # Load iris data set head(iris) # First 6 rows of iris
Table 1: First 6 Rows of the Iris Data Set.
The data table consists of 5 columns, with the following column names:
colnames(iris) # Retrieve all column names # "Sepal.Length" "Sepal.Width" "Petal.Length" "Petal.Width" "Species"
Now, let’s replicate this data for our first example…
data_ex1 <- iris # Replicate iris data for first example
…and replace one of the column names with a new name:
colnames(data_ex1)[colnames(data_ex1) == "Species"] <- "New_Name" # Rename column
With the previous code, we changed the column name Species to New_Name. Let’s print our new column names to the RStudio console to check whether our R code worked well:
colnames(data_ex1) # Check column names after renaming # "Sepal.Length" "Sepal.Width" "Petal.Length" "Petal.Width" "New_Name"
In the first example we renamed only one column – But how could we rename all column names of our data frame?
You guessed it: That’s what I’m going to show you in the next example…
Example 2: Change All R Data Frame Column Names
In the second example, I’ll show you how to modify all column names of a data frame with one line of code. First, let’s create another copy of our iris example data set:
data_ex2 <- iris # Replicate iris data for second example
We can change all variable names of our data as follows:
colnames(data_ex2) <- c("x1", "x2", "x3", "x4", "x5") # Modify column names
Let’s check, if we did it well:
colnames(data_ex2) # Check column names after renaming
colnames(data_ex2) <- c("x1", "x2", "x3", "x4") # The last column is NA colnames(data_ex2) # Check column names again # "x1" "x2" "x3" "x4" NA
Example 3: How to Change Multiple Column Names in R
It is also possible to change only some variable names, but leaving the others as they are. Again, let’s start by replicating the iris data:
data_ex3 <- iris # Replicate iris data for third example
With the following R code, you can replace the two colnames Sepal.Width and Petal.Width by New1 and New2:
colnames(data_ex3)[colnames(data_ex3) # Rename two variable names %in% c("Sepal.Width", "Petal.Width")] <- c("New1", "New2")
Please note that the ordering of the new column names has to reflect the order of the columns in the data frame.
Let’s see if it worked:
colnames(data_ex3) # Check column names after renaming
Video Explanation: Renaming Variables in R
In case you want to see further examples, have a look at the following video of my Statistical Porgramming YouTube channel. In the video, I’m applying the codes of the three previous examples to the airquality data set.
- Change Row Names of Data Frame or Matrix
- select & rename R Functions of dplyr Package
- Subsetting Data Frame in R
- The length Function in R
- The ncol R Function
- NA Values in R
- The R Programming Language