Change more than one column name of a given DataFrame in R
Last Updated :
16 Mar, 2021
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 respecti
6 min read
Change column names and row indexes in Pandas DataFrame Given a Pandas DataFrame, let's see how to change its column names and row indexes. About Pandas DataFramePandas DataFrame are rectangular grids which are used to store data. It is easy to visualize and work with data when stored in dataFrame. It consists of rows and columns.Each row is a measuremen
4 min read
How to change dataframe column names in PySpark ? In this article, we are going to see how to change the column names in the pyspark data frame. Let's create a Dataframe for demonstration: Python3 # Importing necessary libraries from pyspark.sql import SparkSession # Create a spark session spark = SparkSession.builder.appName('pyspark - example jo
3 min read
Add column names to dataframe in Pandas Sometimes, Pandas DataFrames are created without column names, or with generic default names (like 0, 1, 2, etc.). Let's learn how to add column names to DataFrames in Pandas. Adding Column Names Directly to columns Attribute The simplest way to add column names is by directly assigning a list of co
3 min read
How to rename columns in Pandas DataFrame In this article, we will see how to rename column in Pandas DataFrame. The simplest way to rename columns in a Pandas DataFrame is to use the rename() function. This method allows renaming specific columns by passing a dictionary, where keys are the old column names and values are the new column nam
4 min read