You are currently viewing How to Rename Column in R

How to rename a column in the R data frame? There are multiple ways to rename a column(variable) or multiple columns in the R data frame (data.frame). You can use colnames() and names() methods to rename a single column using column index or name and use rename() function from the dplyr package along with "%>%" operator to rename multiple columns.

In this article, I will explain how to rename columns/variables in the R data frame by using the following approaches. Note that in R the data frame columns are called variables and rows are called observations.

1. Quick Examples of Rename Column

Following are quick examples to rename columns/variables of the R data frame (change/update old column name with new column name).


# Below are the quick examples

# Example 1 - Rename second column to c2
colnames(my_dataframe)[2] ="c2"

# Example 2 - Rename fifth column to c5
names(my_dataframe)[5] ="c5"

# Example 3 - Rename the column name - id to c1
colnames(my_dataframe)[colnames(my_dataframe) == "id"] ="c1"

# Example 4 - Load the library
library("dplyr")
# Using rename()
my_dataframe <- my_dataframe %>% 
       rename("id" = "c1")

# Example 5 - Rename multiple columns using rename()
my_dataframe <- my_dataframe %>% 
        rename("id" = "c1",
               "pages" = "c2",
               "name" = "c3")

# Example 6 - Rename column by index
my_dataframe <- my_dataframe %>% 
       rename(col1 = 1, col2 = 2)

# Example 7 - Using select()
my_dataframe <- my_dataframe %>% 
    select(col1 = 1, everything() )

# Example 8 - Using rename_with()
my_dataframe <- my_dataframe %>% 
    rename_with(.cols = 1, ~"col1")

# Example 9 - Load library data.table
library(data.table)
# Rename multiple columns for old to new
setnames(my_dataframe, old = c('c1','c2','c3'), 
         new = c('id','pages','name'))

Let’s create an R data frame, run these examples, and explore the output. If you already have data in CSV you can easily import CSV files to R data frame. Also, refer to Import Excel File into R.


# Create data frame
my_dataframe=data.frame(id=c(11,22,33,44,55),
    pages=c(32,45,33,22,56),
    name=c("spark","python","R","java","jsp"),
    chapters=c(76,86,11,15,7),
    price=c(144,553,321,567,890))

# Display the data frame
print(my_dataframe)

Yields below output.

r rename column

Notice that the column names are: id, pages, name, chapters, and price. You can rename the column name of a single R data frame column using several methods.

2. R Rename Column using colnames()

colnames() is the method available in R base which is used to rename single/multiple/all columns present in the data frame. By using this you can rename a column by column index /column name.

Let’s use this method to rename all the columns presented in the R DataFrame with new column names at a time. for example,


# Rename all old column names to new column names
colnames(my_dataframe) <- c("c1", "c2", "c3", "c4", "c5")
print(my_dataframe)

Yields below output.

r rename column

As we can see from the above code has returned the dataframe with all new column names. Now we will see how to rename the specified column name by its index. For example,

Alternatively, you can also use the name() method to rename the column of the R data frame. Note that indexing starts with 1 in R, not zero like in other languages.

Syntax:


# Syntax using colnames()
colnames(my_dataframe)[index_pos] = "new_column_name"

# Syntax using names()
names(my_dataframe)[index_pos] = "new_column_name"

Where index_pos represent the column index. In this example, we will change the second column pages  to c2 and the fifth column price to c5.


# Change second column pages to c2 using colnames()
colnames(my_dataframe)[2] = "c2"

# Change fifth column to c5 using names()
names(my_dataframe)[5] = "c5"

# Display the dataframe
print(my_dataframe)

Yields below output.

r rename column

We can see that the second and fifth column names are changed.

3. Rename the Column by Name in R

Sometimes you would be required to rename a column by column name in R, when you do by name you don’t have to know the index of the column you would like to change. To do this, you have to specify a condition. We will check by comparing it with the old column name and assigning a new column name to it.

Syntax:


# Syntax rename with condition
colnames(my_dataframe)[colnames(my_dataframe) == "old_column_name"] ="new_column_name"

Let’s run an example.


# Change the column name - id to c1
colnames(my_dataframe)[colnames(my_dataframe) == "id"] = "c1"
# (or)
# Change the column name - id to c1
names(my_dataframe)[names(my_dataframe) == "id"] = "c1"

# Display the dataframe
print(my_dataframe)

Yields the below output.


# Output:
  c1    c2   name chapters    c5
1 11    32  spark       76   144
2 22    45 python       86   553
3 33    33      R       11   321
4 44    22   java       15   567
5 55    56    jsp        7   890 

4. Rename DataFrame Column in R using rename()

rename() is the method available in the dplyr package, which is used to change the particular column name present in the data frame. The operator %>% is used to load the renamed column names to the data frame. Moreover, you can use this function to rename the multiple column names. After using the %>% operator, the subsequent functions are applied to the data frame on the left.

dplyr is a third-party library hence, to use the dplyr library, you need to first install it by using install. packages(‘dplyr’). Once installation is completed, load the dplyr library using library("dplyr").

Syntax:


#Load the library
library("dplyr")
# Use rename()
my_dataframe %>% rename(new_column_name = old_column_name)

where,

  1. my_dataframe is the input data frame
  2. new_column_name is the new column name that replaces the old_column_name

Let’s rename a column from c1 to id by using rename().


# Load library
library("dplyr")

# Change the column name - c2 to pages
my_dataframe %>% 
    rename("id" = "c1")

Yields below output. This changes the col name from c1 to id.


# Output:
  id    c2   name chapters    c5
1 11    32  spark       76   144
2 22    45 python       86   553
3 33    33      R       11   321
4 44    22   java       15   567
5 55    56    jsp        7   890

Note that the above example doesn’t change the column on the existing data frame. print() of this data frame results in unchanged data. To update assign this statement to the existing data frame.


# Load library
library("dplyr")

#Update the column on existing dataframe 
my_dataframe <- my_dataframe %>% 
    rename("id" = "c1")
print(my_dataframe)

Yields the same output as above, but updates the column names on my_dataframe.

5. Change Multiple Columns using rename()

Similarly, you can also rename multiple columns by name using rename() function on the R data frame, here all you need is to know your old and new column names.


# Load library
library("dplyr")

# Rename multiple columns
my_dataframe <- my_dataframe %>% 
                    rename("pages" = "c2",
                           "price" = "c5")
print(my_dataframe)

Yields below output


# Output
  id pages   name chapters price
1 11    32  spark       76   144
2 22    45 python       86   553
3 33    33      R       11   321
4 44    22   java       15   567
5 55    56    jsp        7   890

6. Rename All Columns using rename_with()

rename_with() function is from the R dplyr library that can be used to rename all data frame columns. In the below example let’s use this to convert all column names to upper case.


# Load library
library("dplyr")

# Rename using rename_with()
my_dataframe <- my_dataframe %>%
  rename_with(toupper)

print(my_dataframe)

Yields below output.


# Output
  ID PAGES   NAME CHAPTERS PRICE
1 11    32  spark       76   144
2 22    45 python       86   553
3 33    33      R       11   321
4 44    22   java       15   567
5 55    56    jsp        7   890

7. By using setnames()

You can use setnames() function from data.table library to change columns with list. data.table is also a third-party library hence, you need to first install it by using install.packages('data.table'). Once installation is completed, load the data.table library using library("data.table").


# Rename all columns for old to new
# Rename columns from list
setnames(my_dataframe, new = c('c1','c2','c3','c4','c5'), 
         old = c('ID','PAGES','NAME','CHAPTERS','PRICE'))

Frequently Asked Questions on Rename Column in R

How can I rename a column in a data frame?

You can use the colnames() or names() function to rename a specific column in a data frame. Here’s an example, colnames(df)[colnames(df) == "old_name"] <- "new_name"

How do I rename multiple columns at once?

You can use the rename() function of dplyr package with multiple arguments to rename the multiple columns of data frame at once. For example, df <- df %>% rename(new_name1 = old_name1, new_name2 = old_name2, ...)

What if I want to rename columns based on a vector of new names?

Simply, you can use the colnames() function to rename columns based on a vector of new names. For example, colnames(df) <- c("new_name1", "new_name2", "new_name3",....)

How can I remove spaces from column names?

You can use the make.names() function to remove spaces and make column names syntactically valid. Here’s an example, colnames(df) <- make.names(colnames(df))

8. Conclusion

In this article, you have learned how to rename a single column/variable name, multiple and all columns of the R data frame (data.frame) using the colnames(), names() function through column index and conditionally. Also, we discussed using rename() and setnames().

References

Naveen Nelamali

Naveen Nelamali (NNK) is a Data Engineer with 20+ years of experience in transforming data into actionable insights. Over the years, He has honed his expertise in designing, implementing, and maintaining data pipelines with frameworks like Apache Spark, PySpark, Pandas, R, Hive and Machine Learning. Naveen journey in the field of data engineering has been a continuous learning, innovation, and a strong commitment to data integrity. In this blog, he shares his experiences with the data as he come across. Follow Naveen @ LinkedIn and Medium