# 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") |

# 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 |

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" |

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 |

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 |

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" |

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 example |

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 |

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) # Check column names after renaming

Nice!

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 |

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 |

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") |

colnames(data_ex3)[colnames(data_ex3) # Rename two variable names %in% c("Sepal.Width", "Petal.Width")] <- c("New1", "New2")

Let’s see if it worked:

colnames(data_ex3) # Check column names after renaming |

colnames(data_ex3) # Check column names after renaming

Perfect!

## 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

### Subscribe to my free statistics newsletter:

### R Tutorials

abs Function in R

all & any R Functions

Set Aspect Ratio of Plot

attach & detach R Functions

attr, attributes & structure in R

cbind R Command

Change ggplot2 Legend Title

Character to Numeric in R

Check if Object is Defined

col & row sums, means & medians

Complete Cases in R

Concatenate Vector of Strings

Convert Date to Weekday

cumsum R Function

Data Frame Column to Numeric

diff Command in R

difftime R Function

dim Function in R

dir R Function

Disable Scientific Notation

Draw Segments in R

droplevels R Example

Evaluate an Expression

Extract Characters from String

Factor to Numeric in R

Format Decimal Places

get, get0 & mget in R

is.na R Function

is.null Function in R

jitter R Function

Join Data with dplyr Package

length Function in R

lowess R Smoothing Function

max and min Functions in R

NA Omit in R

nchar R Function

ncol Function in R

nrow Function in R

outer Function in R

pairs & ggpairs Plot

parse, deparse & R expression

paste & paste0 Functions in R

pmax and pmin R Functions

polygon Plots in R

pretty R Function

R Find Missing Values

R Functions List (+ Examples)

R NA – Values

R Replace NA with 0

rbind & rbind.fill in R

Read Excel Files in R

readLines, n.readLines & readline

Remove Element from List

Remove Legend in ggplot2

Rename Column Name in R

Replace Last Comma of String

rev R Command

Round Numeric Data in R

Save & Load RData Workspace

scan R Function

setdiff R Function

setNames vs. setnames in R

sink Command in R

Sort, Order & Rank Data in R

sprintf Function in R

Square Root in R

str_c Function of stringr Package

str_sub Function of stringr Package

strptime & strftime Functions

substr & substring R Commands

sweep R Function

Transform Data Frames

union Function in R

unlist in R

weekdays, months, quarters & julian in R

with & within R Functions

Write Excel File in R