Rename Data Frame Columns In R - Datanovia
Maybe your like
In this tutorial, you will learn how to rename the columns of a data frame in R.This can be done easily using the function rename() [dplyr package]. It’s also possible to use R base functions, but they require more typing.

Contents:
- Required packages
- Demo dataset
- Renaming columns with dplyr::rename()
- Renaming columns with R base functions
- Summary
Required packages
Load the tidyverse packages, which include dplyr:
library(tidyverse)Demo dataset
We’ll use the R built-in iris data set, which we start by converting into a tibble data frame (tbl_df) for easier data analysis.
my_data <- as_tibble(iris) my_data ## # A tibble: 150 x 5 ## Sepal.Length Sepal.Width Petal.Length Petal.Width Species ## <dbl> <dbl> <dbl> <dbl> <fct> ## 1 5.1 3.5 1.4 0.2 setosa ## 2 4.9 3 1.4 0.2 setosa ## 3 4.7 3.2 1.3 0.2 setosa ## 4 4.6 3.1 1.5 0.2 setosa ## 5 5 3.6 1.4 0.2 setosa ## 6 5.4 3.9 1.7 0.4 setosa ## # ... with 144 more rowsRenaming columns with dplyr::rename()
Rename the column Sepal.Length to sepal_length and Sepal.Width to sepal_width:
my_data %>% rename( sepal_length = Sepal.Length, sepal_width = Sepal.Width ) ## # A tibble: 150 x 5 ## sepal_length sepal_width Petal.Length Petal.Width Species ## <dbl> <dbl> <dbl> <dbl> <fct> ## 1 5.1 3.5 1.4 0.2 setosa ## 2 4.9 3 1.4 0.2 setosa ## 3 4.7 3.2 1.3 0.2 setosa ## 4 4.6 3.1 1.5 0.2 setosa ## 5 5 3.6 1.4 0.2 setosa ## 6 5.4 3.9 1.7 0.4 setosa ## # ... with 144 more rowsRenaming columns with R base functions
To rename the column Sepal.Length to sepal_length, the procedure is as follow:
- Get column names using the function names() or colnames()
- Change column names where name = Sepal.Length
It’s also possible to rename by index in names vector as follow:
names(my_data)[1] <- "sepal_length" names(my_data)[2] <- "sepal_width"Summary
In this chapter, we describe how to rename data frame columns using the function rename()[in dplyr package].
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
-
How To Add Header To Pandas Dataframe? - Stack Vidhya
-
How To Add A Column To A Dataframe In R With Tibble & Dplyr
-
R – Rename All Dataframe Column Names - Spark By {Examples}
-
R: Column Names - Apache Spark