You are currently viewing How to Rename Column by Index Position in R?

How to rename column by index in the R data frame? R provides base function colnames() and names() function to change column name by index position. Besides these, use dplyr rename(), select() and rename_with() to rename/change a DataFrame (data.frame) column.

In a separate article, I have covered different ways to rename columns in R if you have time, I would recommend reading it.

1. Quick Example of Rename Column Name by Index Position

Following are quick examples to rename data frame columns by Index position in R.


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

#Example 2 - Change second column to c2 using names()
names(df)[2] = "c2"

# Example 3 - load library
library(dplyr)
# Rename column by index
df <- df %>% 
       rename(col1 = 1, col2 = 2)

# Example 4 - You can also write as
df <- rename(df, col1 = 1)

# Example 5 - Using select()
df <- df %>% 
    select(col1 = 1, everything() )

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

Let’s create an R DataFrame (data.frame) and run the above examples and validate the results.


# Create dataframe
df=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))

# Print dataframe
print(df)

Yields below data frame 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 

2. Rename Column By Index using colnames()

colnames() is the method available in R which is used to rename a single column name present in the data frame. Renaming a single column can be done through the column index. We have to specify the index of the column. In R, indexing starts with 1.

Syntax:


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

Where index_pos represent the column index.

Example: 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(df)[2] = "c2"

#Change fifth column to c5
colnames(df)[5] = "c5"

#Display the dataframe
print(df)

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 Column by Index using names()

Alternatively, you can also use name() method from R base to rename. This method exactly behaves the same as colnames().


#Change second column to c2 using names()
names(df)[2] = "c2"

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

#Display the dataframe
print(df)

Yields the same output as above.

4. Use rename()

Use rename() from R dplyr library to change columns by index in the data frame. This is the best approach as it is easily readable and the code is more organized. Note that dplyr doesn’t change the current data frame instead, it returns the new data frame after the rename.


#Syntax of rename()
df %>%
      rename(new_col_name = col_index)

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"). If you have tidyverse packages installed, load the tidyverse packages, which include dplyr.


# Using rename() from dplyr library

# load library
library(dplyr)

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

# print dataframe
print(df)

Yields below output. When you use %>% operator, the functions we use after this will be applied to the data frame to the left of the operator.


# Output
  col1 col2   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

You can also simply write the statement below to rename multiple columns by index.


# You can also write as
df <- rename(df, col1 = 1, col2 = 2)

5. Use select() Function

Alternatively, you can also use select() to change the column name of the data frame by position. select() also understands column indices, so if you’re renaming the first column, you can simply do as following.


# Using select()
df <- df %>% 
    select(col1 = 1, everything() )
print(df)

Note that using select() changes the order of the columns on the resultant data frame when you rename the column in the middle of the R data frame.

5. Using rename_with()

Finally, use the rename_with() method to rename column name by Index Position in R data frame.


# Using rename_with()
df <- df %>% 
    rename_with(.cols = 1, ~"col1")
print(df)

Conclusion

In this article, you have learned the usage of rename(), select() and rename_with() methods to change a data frame column by index position in R. Besides these R also provides base function colnames() and names() function to change column name by position.

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