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
Note: The replacement vector of column names has to have the same length as the number of columns of our original data. Otherwise, the remaining column names are labelled as NA:
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.
Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party.
If you accept this notice, your choice will be saved and the page will refresh.
- 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
18 Comments. Leave new
Leave a Reply Cancel reply
Statistics Globe Newsletter
Hey, quick thing, in your original example for changing two colnames at a time, you are missing a close square bracket before the assign operator. In the explanation it is correct.
colnames(data)[colnames(data) %in% c(“Old_Name1”, “Old_Name2”) <- c("New_Name1", "New_Name2")
colnames(data)[colnames(data) %in% c("Old_Name1", "Old_Name2")] <- c("New_Name1", "New_Name2")
Thanks a lot for letting me know! I fixed it.
At least for data.table, the method for renaming multiple columns at once does not always work. It seems that
`colnames(data)[colnames(data) %in% c(“Old_Name1”, “Old_Name2”)]` returns the column names in the order they exist in the data.table (which is not necessarily the order of the names in the RHS of %in%), so the vector from which the names are assigned has too have the same order of the columns of the data.table, or columns would be assigned wrong names.
So in order for this to work, the order of the names in the new names vector must be the same as the order of the columns of the data.table. To me this requirement seems to strict.
Thank you for this clarifications. Indeed, the ordering of the new names has to reflect the ordering of the columns in the data frame. I added a clarification to the example.
how can we replace a name with another name in r notebook
Thanks for the question. I have no experience with R notebook, so unfortunately you will have to find the answer somewhere else. Sorry for that!
Nice, thanks buddy.
You are very welcome Kev! 🙂
I am a complete newcomer to R. I have copied excel data created by me into R. Now I would like to rename the data frame below into “Training” in order to practice descriptive analysis and logistic regression.
Besides, the variable Gender is already coded in 0 for female and one for male, instead of female and male. Is that ok? Or do I have to put into a specific R code as well?
Could you help me with the renaming code?
Thanks a lot for your help.
# A tibble: 49 x 8
ID Gender Alter Schule Arbeit Partei Umwelt_B COVID_UMG
1 1 0 20 Abitur 0 GRÜN 1 0
2 2 1 45 Realschule 0 AfD 0 0
3 3 0 17 Gymnasium 0 keine 1 1
4 4 1 30 HS 1 CDU 1 1
5 5 1 40 HS 1 CDU 1 1
6 6 0 30 Gymnasium 1 SPD 1 0
7 7 0 27 Realschule 0 AfD 0 0
8 8 1 55 HS 1 FDP 1 1
9 9 1 27 Abitur 0 FDP 1 1
10 10 1 37 Realschule 0 SPD 0 0
# … with 39 more rows
You have to store the imported data in a data object. You can do that using the assignment arrow “<-" as shown below:
The previous R code has created a data object called my_data. Now, you can work with this data object by using the name my_data instead of the name of your original Excel file.
I hope that helps!
And there you can see, Joachim, why I cannot proceed with my intention:
Fehler in summary(REG_CSV.csv) : Objekt ‘REG_CSV.csv’ nicht gefunden / not found
In data(REG_CSV.csv) : Datensatz ‘REG_CSV.csv’ nicht gefunden (not found)
In other words, the excel file has been imported, but I cannot find it under the old name it had before…
Please see the answer to your other question 🙂
Thanks a lot, indeed, Joachim.
Not I can go ahead and hold tight to my dream about learning how to use R for data analysis.
That’s great to hear Marcus, good luck with learning R! 🙂
Sorry for bothering with something so obvious, but here is my problem still.
The fictitious data below should be binary, meaning almost all answers should be coded 0=no and 1=yes, or 0=female and 1=male. When I run the function summary (), that is not what I get, as you can see below.
I have tried the copied code
dat <- my_data(sex=sample(c("Frau", "Mann"), 10, replace=TRUE))
t$sex.n <- as.numeric(
levels = c("Frau", "Mann"),
labels = c("0", "1"))))
But it did not go well either.
Any simple suggestions?
Thanks in advance. I shall not bother again!
ID Gender Alter Schule
Min. : 1.00 Length:49 Min. :17.00 Length:49
1st Qu.:13.00 Class :character 1st Qu.:26.00 Class :character
Median :26.00 Mode :character Median :35.00 Mode :character
Mean :25.69 Mean :35.16
3rd Qu.:38.00 3rd Qu.:43.00
Max. :50.00 Max. :60.00
Arbeit Partei Umwelt_B COVID_Umgang
Length:49 Length:49 Length:49 Length:49
Class :character Class :character Class :character Class :character
Mode :character Mode :character Mode :character Mode :character
FlüchtAufnah deutlich weniger Überprüfunggründe
Length:49 Length:49 Length:49
Class :character Class :character Class :character
Mode :character Mode :character Mode :character
I would apply the table() function to your column to see all values that are stored in this column:
Then, I would replace the values you don’t want to keep in this column as explained here:
I hope that helps!
Thank you this helped a lot!!
very well explained
thank you very much for the positive feedback! Great to hear that you liked the tutorial!