Skip to content
Related Articles

Related Articles

Improve Article

Change more than one column name of a given DataFrame in R

  • Last Updated : 16 Mar, 2021
Geek Week

A data frame is a tabular structure with fixed dimensions, of each row as well as columns. It is a two-dimensional array-like object with numerical, character-based, or factor-type data. Each element belonging to the data frame is indexed by a unique combination of the row and column number respectively. Column names are addressed by unique names.

Method 1: Using colnames() method

colnames() method in R is used to rename and replace the column names of the data frame in R.

The columns of the data frame can be renamed by specifying the new column names as a vector. The new name replaces the corresponding old name of the column in the data frame. The length of the new column vector should be equivalent to the number of columns originally. Changes are made to the original data frame.

Syntax:

colnames(df) <- c(new_col1_name,new_col2_name.. )



1(A). Replacing a range of columns

Replacing more than one column name can be done by using the colon separator beginning with the start column index to replace and ending with the last column index (inclusive) to be renamed. Time complexity is directly proportional to the number of columns. 

Example:

R




# declaring the columns of data frame
df = data.frame(
c1 = c(1:3),
c2 = c(5:7),
c3 = c(12:14))
  
# printing original data frame
print("Original data frame : ")
print(df)
  
# renaming all the column names of data frame
colnames(df)[2:3] <- c("Col2","Col3")
  
print("Renamed data frame : ")
print(df)

Output

[1] “Original data frame : “

 c1 c2 c3

1  1  5 12



2  2  6 13

3  3  7 14

[1] “Renamed data frame : “

 c1 Col2 Col3

1  1    5   12

2  2    6   13

3  3    7   14

1(B). Changing all the column names

For this simply pass the names of the new columns as parameter to function

Example:

R




# declaring the columns of data frame
df = data.frame(
c1 = c(1:3),
c2 = c(5:7),
c3 = c(12:14))
  
# printing original data frame
print("Original data frame : ")
print(df)
  
# renaming all the column names of data frame
colnames(df) <- c("Col1","Col2","Col3")
print("Renamed data frame : ")
print(df)

Output

[1] “Original data frame : “

 c1 c2 c3

1  1  5 12

2  2  6 13

3  3  7 14

[1] “Renamed data frame : “

 Col1 Col2 Col3

1    1    5   12



2    2    6   13

3    3    7   14

Method 2: Using setNames() method

setNames() method in R can also be used to assign new names to the columns contained within a list, vector or tuple. The changes have to be saved back then to the original data frame, because they are not retained. Time complexity required to replace the names is equivalent to the number of columns to be renamed. 

Syntax:

setname(df, name of columns)

Example:

R




# declaring the columns of data frame
df = data.frame(
c1 = c(1:3),
c2 = c(5:7),
c3 = c(12:14))
  
# printing original data frame
print("Original data frame : ")
print(df)
  
# renaming all the column names of data frame
df <- setNames(df, c("changed_Col1","changed_Col2","changed_Col3"))
print("Renamed data frame : ")
print(df)

Output

[1] “Original data frame : “

 c1 c2 c3



1  1  5 12

2  2  6 13

3  3  7 14

[1] “Renaming columns names “

[1] “Renamed data frame : “

 changed_Col1 changed_Col2 changed_Col3

1            1            5           12

2            2            6           13

3            3            7           14

Method 3: Using names() method

names() method works similarly to colnames() method and then can be used to replace or rename an individual, a subset or all the column names of the data frame. 

Syntax:

names(df)[range] <- c(name of the columns)

Example:

R




# declaring the columns of data frame
df = data.frame(
c1 = c(1:3),
c2 = c(5:7),
c3 = c(12:14),
c4 = c(67:69),
c5 = c(98:100))
  
# printing original data frame
print("Original data frame : ")
print(df)
  
# renaming  the subset of column names  of data frame
names(df)[2:4] <- c("Col2","Col3","Col4")
print("Renamed data frame : ")
print(df)

Output

[1] “Original data frame : “

 c1 c2 c3 c4  c5

1  1  5 12 67  98

2  2  6 13 68  99

3  3  7 14 69 100

[1] “Renamed data frame : “

 c1 Col2 Col3 Col4  c5

1  1    5   12   67  98

2  2    6   13   68  99

3  3    7   14   69 100




My Personal Notes arrow_drop_up
Recommended Articles
Page :