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:
|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|
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
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
colnames(), which take a data frame as an argument and return a vector containing the column names.
dogs data frame from the previous exercise is given to you in
notebook.Rmd. Save the column names of
original_col_names and print it.
Update the name of
popularity_change_13_to_16. Save the updated data frame to
Save the new column names of
new_col_names and print it.