Many times we are required to change the column names of a dataframe for the analysis. In this blog, we’ll see the common dplyr functions using which we can easily change the column names. We’ll be using ‘iris’ dataset here which is a built-in dataset in R.
- select()
We can rename the columns using select() function which will select the columns and at the ame time change their names by providing a new name on the left-hand side of an equals operator (=).
#Rename Sepal.Width column to sepal_width and Species to species
iris %>%select(sepal_width=Sepal.Width, species=Species)
2. rename()
If you want to retain all the columns but with some renamed, you can use the rename() function. rename() will output all the columns with the names adjusted for mentioned columns.
#Select all the columns but rename Sepal.Width column to sepal_width and Species to species
iris %>%rename(sepal_width=Sepal.Width, species=Species)
3. Variations of rename() function
We can use *_at(), *_if(), and *_all() versions of rename() function to change some or all the columns.
#Load library stringr
library(stringr)
#Rename all the columns to lowercase
iris %>%rename_all(str_to_lower)
#Rename only the numeric columns to lowercase
iris %>% rename_if(is.numeric, str_to_lower)
#Rename all the columns to lowercase which starts with 'S'
iris %>%rename_at(vars(starts_with("S")), str_to_lower)
4. Convert row_names to column
#Load the library tidyverse
library(tidyverse)
#Convert rownames in mtcars dataset to column 'car'
mtcars %>%rownames_to_column("car") #rownames_to_column comes from tibble package which gets loaded with tidyverse
Thank you for reading 🙂
References: https://itsalocke.com/files/DataManipulationinR.pdf
Kanata Due Diligence
Great topic, please keep it up I really enjoy reading these type articles.