R Tutorial: The mutate verb

Learn how to use dplyr's mutate verb to create and modify columns in R. The tutorial shows converting population to millions, calculating total GDP by multiplying population by GDP per capita, then combining mutate with filter and arrange to find the countries with highest GDP in 2007: United States, China, Japan, India, and Germany.
Chapters
You have already learned two verbs for manipulating Filter for extracting a subset of the observations, and a range for sorting them. Suppose you want to change one of the variables in your data set based on the other ones. Or suppose you want to add a new variable. For that, you would use the mutate verb. First, you'll learn how to change an existing variable. You use mutate like you would filter or Arrange after a pipe operator. Inside this mutate statement, what's on the right of the equals sign is what's being calculated, and what's on the left is what's being replaced. Here you're calculating pop/1 million. The slash in R performs division, so population divided by 1 million. On the left, you you're saying you want to replace the existing POP column by writing pop. The result is the same table but with POP replaced with a new value, one that's much smaller than it was before. This is how you manipulate existing variables in the table. That's often necessary during data processing and cleaning. Just like Filter and Arrange, you're not altering the original Gapminder data. You're just changing the value in this new data frame that's being returned. Alternatively, you may want to add a new variable. For instance, you currently have the GDP per capita. That's the gross domestic product of the country divided by the current population. But in your analysis, you may want to know the total gdp. You'd have to multiply the population by the GDP per capita. To find this, you would use mutate in almost the same way. You pipe your Gapminder data to the mutate verb. The asterisk in R represents multiplication. So you write GDP per cap POP to multiply the two columns. Notice that to be informative, we've named the column gdp. That's what's to the left of our equal sign in our code. Column names need to be one word, like this. You can't use spaces. Notice that in the results there's a brand new GDP column which is much larger than the GDP per capita. Let's put together the three verbs you learned in this chapter to answer a question about our data. Suppose we wanted to know the countries with the highest total GDP in the year 2007. We would do this in three steps. Create the column fill filter for 2007, then sort. We use mutate to create the total GDP column. Then we use filter to restrict our analysis to only the year 2007. Then we use arrange to sort the data in descending order of our new GDP variable. This gives us the answer we're looking for. The United States was the country with the highest total GDP in 2007 with a GDP of $13 trillion. The US is followed by China, Japan, India and Germany as the highest GDP countries. Once you become comfortable with dplyr, you'll be able to find answers like these and answer much more complicated questions on your own data.
More from Richie

#359 My Best Friend is AI with Valerie Tiberius, Professor of Philosophy at University of Minnesota

How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon

#358 How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon







