In the dplyr
package, we use the rename()
function to rename each variable or column name individually. It changes the column names, but the column order is preserved along with DataFrame attributes.
rename(.data, ...)
It takes the following argument values.
.data
: This can be a DataFrame, a tibble, or a lazy DataFrame....
: This is an additional argument, used to rename variables names as new_name=old_name
.It returns an updated object of the same type as .data
.
In this code snippet, we'll discuss the rename()
function from the dplyr
package to rename the column names of the iris dataset.
library("dplyr")# Load iris dataset as R tibbleiris <- as_tibble(iris)# Print loaded datasetprint(iris)# update new_name = old_name# as petal_length = Petal.Lengthrename(iris, petal_length = Petal.Length)
as_tibble(iris)
keyword will load the iris dataset as an R tibble in the iris
variable.rename(iris, petal_length = Petal.Length)
which will change the current variable name Petal.Length
to Petal_Length
.