The mutate function is used anytime you want create or modify a variable. It works with pretty much any R function that creates/modifies variables, so you can wrap it around code to create factors, base R code for descriptive statistics (like median, mean, standard deviation), convert a character to a number (or vice versa), compute a date difference, and so on, as well as use any arithmetic or logical operations. You can include multiple new and/or modified variables in your mutate function, and even create or change variables using ones you created in the same mutate command. Let's demonstrate, again with the reading dataset (if you're playing along at home, once again, you can download that file here).
reads2019 <- read_csv("~/Downloads/Blogging A to Z/SaraReads2019_allrated.csv", col_names = TRUE)
reads2019 <- reads2019 %>% mutate(Fiction = factor(Fiction, levels = c(0,1), labels = c("Non-Fiction", "Fiction")), Fantasy = factor(Fantasy, levels = c(0,1), labels = c("Non-Fantasy", "Fantasy")), SciFi = factor(SciFi, levels = c(0,1), labels = c("Non-Science Fiction", "Science Fiction")), date_started = as.Date(reads2019$date_started, format = '%m/%d/%Y'), date_read = as.Date(date_read, format = '%m/%d/%Y'), StartDay = wday(date_started, label = TRUE, abbr = FALSE), FinishDay = wday(date_read, label = TRUE, abbr = FALSE), Age = ifelse(OriginalPublicationYear >= 2015, 1, 0), Age = factor(Age, levels = c(0,1), labels = c("New Publication", "Older Publication")))
data <- data %>%
mutate(variable = f(variable))
Sure, it seems more verbose when you're doing one quick change, but changing datasets often involve multiple variables, and may require you to use mutate with filters or group_bys. That makes tidyverse much more powerful and cleaner.
Things are heating up this week at work and I'm dangerously behind on blog posts - I wrote a bunch in advance, but got busy and am now writing this post only the evening before it goes live. Hoping to do some catchup later tonight, but Thursday's post (and/or later posts in the series) may be slightly delayed. Don't worry! We'll make it through A to Z somehow!