From the course: Learning the R Tidyverse

Unlock the full course today

Join today to access over 24,100 courses taught by industry experts.

Using the mutate() function to modify and add columns

Using the mutate() function to modify and add columns - R Tutorial

From the course: Learning the R Tidyverse

Using the mutate() function to modify and add columns

- [Instructor] mutate() is your friend when it comes to wrangling data sets with a tidyverse and is from the dplyr package. mutate() allows us to modify existing columns or to create new columns in data.frame and tibble. Let's take a look at how to use the function in the 01_10b project. At the top of the script, we're loading the tidyverse as always, and then on lines 11 and 12, I've got an advanced option that I'll get to after the basics of using mutate(). So, we're going to be looking at the msleep dataset. Let's print that to the console and make the console a little bit bigger, and we can see that we've got many columns about sleep. What we don't have at the moment is we don't have a non-REM sleep column. So we have sleep_total, we have sleep_rem, but we don't have a non-REM sleep. So, let's create that new column with mutate(). We'll pipe our dataset and sleep into the mutate() function and we're going to create a new column, sleep_non_rem, and we'll calculate sleep_total. You…

Contents