How to Rename Column in R

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

In this article, I will explain different ways how to rename columns/variables in the R data frame, which basically covers 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).


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

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

#Example 3 - Change 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 DataFrame, run these examples and explore the output. If you already have data in CSV you can easily import CSV files to R DataFrame. Also, refer to Import Excel File into R.


# Create dataframe
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 dataframe
print(my_dataframe)

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  

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

2. Rename Column using colnames()

colnames() is the method available in R base which is used to rename columns/variables present in the data frame. By using this you can rename a column by index and name. Alternatively, you can also use name() method. Note that in R, indexing starts with 1 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 id  to c2 and the fifth column price to c5.


#Change second column 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)

Output:


#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

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

3. Rename by Name in R

Sometimes you would be required to rename a column/variable by name, when you do by name you don’t have to know the index of the column you would like to change. In order to do this, you have to specify a condition. We will check by comparing 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 Column in R Dataframe 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. At a time we can change single or multiple column names. When you use %>% operator, the functions we use after this will be applied to the data frame to the left of the operator.

dplyr is a third-party library hence, in order to use dplyr library, you need to first install it by using install.packages('dplyr'). Once installation completes, 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 - c1 to id
my_dataframe %>% 
    rename("id" = "c1")

Yields below output. This changes 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. In order 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, we can also rename multiple columns by name using rename() function on R data frame, here all you need is you should know your old column name and the new column name.


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

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 completes, 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'))

8. Conclusion

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

Related Articles:

References

NNK

SparkByExamples.com is a Big Data and Spark examples community page, all examples are simple and easy to understand and well tested in our development environment Read more ..

Leave a Reply

You are currently viewing How to Rename Column in R