Deals Of The Week - hours only!Up to 80% off on all courses and bundles.-Close
Create new variables
1. New columns
Grouping and statistical functions
Joining datasets
Summary

Instruction

Welcome to the new part of the tidyverse course. In this part, we'll further extend your knowledge of dplyr. Let's create some new variables for the dataset. To do this, we will use the mutate() function. The first argument in mutate() is the dataset. This is followed by the operations we want to perform.

For example, if we wanted to divide one column by another and store the result in a new column named result, we would write:

mutate(dataset, result = column1 / column2)

Let's say we want to calculate the population density for the countries in our dataset and store the results in a new column named density. We would write:

mutate(countries, density = population / area)

Exercise

Use the population and urban_population columns to calculate the urban population percentage for each country in countries. Use the mutate() function, and set urban_pop_pc as the new column name. Overwrite the countries variable.

Stuck? Here's a hint!

Type:

countries <- mutate(countries, urban_pop_pc = (urban_population / population) * 100)