Modifying Data Frames in R
Rename Columns

Since dplyr functions operate on data frames using column names, it is often useful to update the column names of a data frame so they are as clear and meaningful as possible. dplyr’s rename() function allows you to easily do this.

Say you have a data frame of books, as shown in the table below:

name written_by
The Lord of the Rings J. R. R. Tolkien
Le Petit Prince Antoine de Saint-Exupéry
Harry Potter and the Philosopher’s Stone J. K. Rowling
紅樓夢/红楼梦 Cao Xueqin

rename() can take any number of arguments, where each new column name is assigned to replace an old column name in the format new_column_name = old_column_name. rename() returns a new data frame with the updated column names.

To update the name column to book_title and the written_by column to author:

df %>% rename(book_title = name, author = written_by)

You can confirm the names of the columns have been updated using either of the base R functions names() or colnames(), which take a data frame as an argument and return a vector containing the column names.



The updated dogs data frame from the previous exercise is given to you in notebook.Rmd. Save the column names of dogs to original_col_names and print it.


Update the name of avg_height to avg_height_inches, avg_weight to avg_weight_lbs, and rank_change_13_to_16 to popularity_change_13_to_16. Save the updated data frame to dogs.


Save the new column names of dogs to new_col_names and print it.

Folder Icon

Take this course for free

Already have an account?