Change 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 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.
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 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,new_col3_name)
Example:
R
# declaring the columns of data frame
df = data.frame(
col1 = c('A', 'B', 'C', 'J', 'E', NA,'M'),
col2 = c(12.5, 9, 16.5, NA, 9, 20, 14.5),
col3 = c(NA, 3, 2, NA, 1, NA, 0))
# printing original data frame
print("Original data frame : ")
print(df)
print("Renaming columns names ")
# assigning new names to the columns of the data frame
colnames(df) <- c('C1','C2','C3')
# printing new data frame
print("New data frame : ")
print(df)
Output:
[1] "Original data frame : "
col1 col2 col3
1 A 12.5 NA
2 B 9.0 3
3 C 16.5 2
4 J NA NA
5 E 9.0 1
6 <NA> 20.0 NA
7 M 14.5 0
[1] "Renaming columns names "
[1] "New data frame : "
C1 C2 C3
1 A 12.5 NA
2 B 9.0 3
3 C 16.5 2
4 J NA NA
5 E 9.0 1
6 <NA> 20.0 NA
7 M 14.5 0
1(A) .Specific columns of the data frame can also be renamed using the position index of the respective column.
Syntax:
colnames(df)[col_indx] <- "new_col_name_at_col_indx"
Approach
- Create dataframe
- Select the column to be renamed by index
- Provide a suitable name
- Change using colnames() function
Example:
R
# declaring the columns of data frame
df = data.frame(
col1 = c('A', 'B', 'C', 'J', 'E', NA,'M'),
col2 = c(12.5, 9, 16.5, NA, 9, 20, 14.5),
col3 = c(NA, 3, 2, NA, 1, NA, 0))
# printing original data frame
print("Original data frame : ")
print(df)
print("Renaming columns names ")
# assigning the second column name to a new name
colnames(df)[2] <- "new_col2"
# printing new data frame
print("New data frame : ")
print(df)
Output:
[1] "Original data frame : "
col1 col2 col3
1 A 12.5 NA
2 B 9.0 3
3 C 16.5 2
4 J NA NA
5 E 9.0 1
6 <NA> 20.0 NA
7 M 14.5 0
[1] "Renaming columns names "
[1] "New data frame : "
col1 new_col2 col3
1 A 12.5 NA
2 B 9.0 3
3 C 16.5 2
4 J NA NA
5 E 9.0 1
6 <NA> 20.0 NA
7 M 14.5 0
1(B). Column names can also be replaced by using the which(names(df)) function, which searches for the column with the specified old name and then replaces it with the new specified name instance.
Syntax:
colnames(dataframe)[which(names(dataframe) == "oldColName")] <- "newColName"
Approach
- Create data frame
- Select name of the columns to be changed
- Provide a suitable name
- Use the function
Example:
R
# declaring the columns of data frame
df = data.frame(
col1 = c('A', 'B', 'C', NA,'M'),
col2 = c(12.5, 9, 16.5, 20, 14.5),
col3 = c(NA, 3, 2, NA, 0))
# printing original data frame
print("Original data frame : ")
print(df)
print("Renaming columns names ")
# assigning the second column name to a new name
colnames(df)[2] <- "new_col2"
# printing new data frame
print("After changing the data frame col2 name : ")
print(df)
# replacing first column name
colnames(df)[which(names(df) == "col1")] <- "new_col1"
# printing new data frame
print("After changing the data frame col1 name : ")
print(df)
Output
[1] "Original data frame : "
col1 col2 col3
1 A 12.5 NA
2 B 9.0 3
3 C 16.5 2
4 <NA> 20.0 NA
5 M 14.5 0
[1] "Renaming columns names "
[1] "After changing the data frame col2 name : "
col1 new_col2 col3
1 A 12.5 NA
2 B 9.0 3
3 C 16.5 2
4 <NA> 20.0 NA
5 M 14.5 0
[1] "After changing the data frame col1 name : "
new_col1 new_col2 col3
1 A 12.5 NA
2 B 9.0 3
3 C 16.5 2
4 <NA> 20.0 NA
5 M 14.5 0
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.
Syntax:
setnames(df, c(names of new columns))
Approach
- Create data frame
- Rename column using function
- Display modified data frame
Example:
R
# declaring the columns of data frame
df = data.frame(
col1 = c('A', 'B', 'C', NA,'M'),
col2 = c(12.5, 9, 16.5, 20, 14.5),
col3 = c(NA, 3, 2, NA, 0))
# printing original data frame
print("Original data frame : ")
print(df)
# print("Renaming columns names ")
# 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 : "
col1 col2 col3
1 A 12.5 NA
2 B 9.0 3
3 C 16.5 2
4 <NA> 20.0 NA
5 M 14.5 0
[1] "Renamed data frame : "
changed_Col1 changed_Col2 changed_Col3
1 A 12.5 NA
2 B 9.0 3
3 C 16.5 2
4 <NA> 20.0 NA
5 M 14.5 0
Similar Reads
Change more than one column name of a given DataFrame in R
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 respecti
4 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
How to get name of dataframe column in PySpark ?
In this article, we will discuss how to get the name of the Dataframe column in PySpark. To get the name of the columns present in the Dataframe we are using the columns function through this function we will get the list of all the column names present in the Dataframe. Syntax: df.columns We can a
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
Get Column Index in Data Frame by Variable Name in R
R is an open-source programming language that is used as a statistical software and data analysis tool. In R Programming Language we can work on specific columns based on their names. In this article, we will learn different methods to extract the Get Column Index in Data Frame by Variable Name in R
5 min read
How to Get Column Names in Pandas Dataframe
While analyzing the real datasets which are often very huge in size, we might need to get the pandas column names in order to perform certain operations. The simplest way to get column names in Pandas is by using the .columns attribute of a DataFrame. Let's understand with a quick example:Pythonimpo
4 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
How to name aggregate columns in PySpark DataFrame ?
In this article, we are going to see how to name aggregate columns in the Pyspark dataframe. We can do this by using alias after groupBy(). groupBy() is used to join two columns and it is used to aggregate the columns, alias is used to change the name of the new column which is formed by grouping da
2 min read
How to add multiple columns to a data.frame in R?
In R Language adding multiple columns to a data.frame can be done in several ways. Below, we will explore different methods to accomplish this, using some practical examples. We will use the base R approach, as well as the dplyr package from the tidyverse collection of packages.Understanding Data Fr
4 min read