Open In App

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

Last Updated : 16 Mar, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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



Similar Reads

Change column name of a given DataFrame in R
A data frame is a tabular structure with fixed dimensions, of each rows 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.
5 min read
Extract specific column from a DataFrame using column name in R
In this article, we are going to see how to extract a specific column from a dataframe using the column name in R Programming Language. In the data.frame() we have to pass dataframe_name followed by $ symbol followed by column name. The reason to pass dataframe_name$ column name to data.frame() is, after extracting the data from column we have to s
5 min read
Select Rows if Value in One Column is Smaller Than in Another in R Dataframe
In this article, we will discuss how to select rows if the value in one column is smaller than another in dataframe in R programming language. Data frame in use: Method 1: Using Square Brackets By using &lt; operator inside the square bracket we can return the required rows. Syntax: dataframe[dataframe$column1 &lt; dataframe$column2,] where, datafr
2 min read
Drop column(s) by name from a given DataFrame in R
Dropping of columns from a data frame is simply used to remove the unwanted columns in the data frame. In this article, we will be discussing the two different approaches to drop columns by name from a given Data Frame in R. The different approaches to drop columns by the name from a data frame is R language are discussed below Method 1: Using subs
3 min read
Reorder DataFrame by column name in R
It is very difficult any time taking task if we reorder the column name, so we use R Programming Language to do it effectively. In this article, we will be discussing the three different ways to reorder a given DataFrame by column name in R. Method 1: Manually selecting the new order of the column names according to the user In this particular meth
2 min read
Sort DataFrame by column name in R
Sorting is the process of ordering items. It can be ascending order, descending order, alphabetical order, numerical order. To sort a DataFrame by column name in R programming, we can use various methods as discussed below. To get a better understanding of how to sort DataFrame by column name, let's take some examples. Example: Let's suppose we hav
2 min read
Return Column Name of Largest Value for Each Row in R DataFrame
In this article, we will discuss how to return column names of the largest value for each row in DataFrame in R Programming Language. Example: Column1Column2Column3Max columnRow1200Column1 , Because, Column2 value and Column 3 value is less than Column1Row2435Column3 , Because, Column2 value and Column 1 value is less than Column3Row31066Column1 ,
2 min read
How to change row values based on a column value in R dataframe ?
In this article, we will see how to change the values in rows based on the column values in Dataframe in R Programming Language. Syntax: df[expression ,] &lt;- newrowvalue Arguments : df - Data frame to simulate the modification uponexpression - Expression to evaluate the cell data based on a column valuenewrowvalue - The modified value to replace
4 min read
Remove rows with NA in one column of R DataFrame
Columns of DataFrame in R Programming Language can have empty values represented by NA. In this article, we are going to see how to remove rows with NA in one column. We will see various approaches to remove rows with NA values. Approach Create a data frameSelect the column based on which rows are to be removedTraverse the column searching for na v
2 min read
Append one dataframe to the end of another dataframe in R
In this article, we are going to append the data frame to another data frame using "$" operator in R Programming Language. Approach Create vectorsCreate one dataframe (dataframe1) by passing these vectorsCreate another dataframe (dataframe2)by passing these vectorsFinally, append the dataframe2 to dataframe1 using" $" operator.Display resultant dat
2 min read
Article Tags :