Rename Data Frame Columns In R - Datanovia

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.

Renaming Columns of a Data Table in R

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 rows

Renaming 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 rows

Renaming columns with R base functions

To rename the column Sepal.Length to sepal_length, the procedure is as follow:

  1. Get column names using the function names() or colnames()
  2. Change column names where name = Sepal.Length
# get column names colnames(my_data) ## [1] "Sepal.Length" "Sepal.Width" "Petal.Length" "Petal.Width" ## [5] "Species" # Rename column where names is "Sepal.Length" names(my_data)[names(my_data) == "Sepal.Length"] <- "sepal_length" names(my_data)[names(my_data) == "Sepal.Width"] <- "sepal_width" 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 rows

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 » How To Rename Columns In R