R – Rename All Dataframe Column Names - Spark By {Examples}
Maybe your like
How to rename all columns (column names) at a time in R dataframe (data.frame)? From R base functionality, we have colnames() and names() functions that can be used to rename all columns (column names). These functions are used to set or get the names of a dataframe object. You can also use setnames() to change all old column names with new columns.
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 Examples of Rename All Column Names
Below are quick examples to rename all column names at a time in R.
# Rename all column names names(my_dataframe) <- c('c1','c2','c3','c4','c5') # Rename all column names colnames(my_dataframe) <- c('c1','c2','c3','c4','c5') # Using data.table library(data.table) # rename all columns for old to new # Rename columns from list setnames(my_dataframe, old = c('c1','c2','c3','c4','c5'), new = c('id','pages','name','chapters','price'))Let’s create an R DataFrame (data.frame) and run the above examples and validate the results.
# Create dataframe my_dataframe=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)) #display the dataframe print(my_dataframe)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 8902. Rename All Column Names Using names() in R
In R, names() is the method is used to rename all column names (list with column names). You can also use this method to rename dataframe column by index in R.
Following is the syntax of the names() to use column names from the list.
# Syntax using names() names(my_dataframe) <- c(list_col_names)Here c() returns a one-dimensional vector with the column names and names() is used to set the names to a dataframe. In the below example we are assigning a vector of names to dataframe column names.
# Rename all column names names(my_dataframe) <- c('c1','c2','c3','c4','c5') print(my_dataframe)Yields below output. You can also have the columns with a list to rename.
# Output c1 c2 c3 c4 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 8903. Rename All Column Names Using colnames()
colnames() is the method available in R which can be used to rename all column names (list with column names). Actually, colnames() is used to set or get the names of a dataframe. You can also use this method to rename dataframe column by index in R.
Following is the syntax of the colnames() to use column names from the list.
# Syntax using names() colnames(my_dataframe) <- c(list_col_names)Here c() returns a one-dimensional vector with the column names and colnames() is used to set the names to a dataframe. In the below example we are assigning a vector of names to dataframe column names. Let’s see an example.
# Rename all columns colnames(my_dataframe) <- c('c1','c2','c3','c4','c5') print(my_dataframe)Yields below output.
# Output c1 c2 c3 c4 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 8904. Rename All Columns Using setnames() in R
setnames() is a function from data.table library that can be used to change all column names from the list.
data.table is a third-party library hence, in order to use data.table library, you need to first install it by using install.packages('data.table'). Once installation completes, load the data.table library in order to use this setnames() method. To load a library in R use library("data.table").
# Using data.table library(data.table) # Column names in list old_names=list() new_names=list() # Rename all columns setnames(my_dataframe, old = c('c1','c2','c3','c4','c5'), new = c('id','pages','name','chapters','price')) #Display column names print(colnames(my_dataframe))Yields below output.
# Output column names [1] "id" "pages" "name" "chapters" "price"5. Rename All Columns using rename_with()
rename_with() function is from R dplyr library that can be used to rename all dataframe columns. In the below example let’s use this to convert all column names to upper case.
#Load the library library("dplyr") # Rename using rename_with() my_dataframe <- my_dataframe %>% rename_with(toupper) print(my_dataframe)Yields below 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 8906. Conclusion
In this article, you have learned how to rename all dataframe column names in R. From R base functionality, we have colnames() and names() functions that can be used to rename all columns. These functions are used to set or get the names of a dataframe object.
Related Articles
- dplyr Rename() – To Change Column Name
- Rename Object or Variable in R
- How to Rename File in R? Using file.rename()
- How to Rename Multiple Columns in R?
- Rename Columns With List in R
- How to Rename Column by Index Position in R?
- How to Rename Column in R
- R Sort DataFrame Rows by Column Value
References
- https://www.rdocumentation.org/packages/base/versions/3.6.2/topics/names
Tag » Add Column Names To Data Frame R
-
8.4 Dataframe Column Names - YaRrr! The Pirate's Guide To R
-
Changing Column Names Of A Data Frame - Stack Overflow
-
Change Column Name Of A Given DataFrame In R - GeeksforGeeks
-
How To Add Header To Dataframe In R ? - GeeksforGeeks
-
Add Header To Data Frame In R (Example) - Statistics Globe
-
How To Add A Column To A Data Frame In R (With Examples)
-
How To Add Name To Data Frame Columns In R? - Tutorialspoint
-
How To Set Column Names In R Code Example - Code Grepper
-
Row And Column Names - R
-
R Data Frame: How To Create, Append, Select & Subset - Guru99
-
Rename Data Frame Columns In R - Datanovia
-
How To Add Header To Pandas Dataframe? - Stack Vidhya
-
How To Add A Column To A Dataframe In R With Tibble & Dplyr
-
R: Column Names - Apache Spark