Rename Column Name In R | 3 Examples To Change Data Frame ...
Maybe your like
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 columnWith 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"Looks good!
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 exampleWe can change all variable names of our data as follows:
colnames(data_ex2) <- c("x1", "x2", "x3", "x4", "x5") # Modify column namesLet’s check, if we did it well:
colnames(data_ex2) # Check column names after renamingNice!
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" NAExample 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 exampleWith 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 renamingPerfect!
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.
Further Reading
- 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
Subscribe to the Statistics Globe Newsletter
Get regular updates on the latest tutorials, offers & news at Statistics Globe. I hate spam & you may opt out anytime: Privacy Policy.
Tag » How To Rename Columns In R
-
Rename Data Frame Columns In R - Datanovia
-
How To Rename Column (or Columns) In R With Dplyr - Erik Marsja
-
How To Rename A Single Column In A ame? - Stack Overflow
-
Rename Columns - Dplyr
-
How To Rename Column In R - Spark By {Examples}
-
How To Rename Column By Index Position In R?
-
Change Column Name Of A Given DataFrame In R - GeeksforGeeks
-
3 Ways To Rename (Multiple) Columns In R
-
Rename The Column Name In R Using Dplyr
-
Renaming Columns In A Data Frame - Cookbook For R
-
Add, Remove, & Rename Columns In R Using Dplyr - Enterprise DNA
-
How To Rename Columns In R Data Frame? - Tutorial Kart
-
Rename The Data Frame Columns | R - DataCamp
-
How To Rename Columns In R - DBACLASS