Create new variables
3. Combining mutate() and if_else()
Grouping and statistical functions
Joining datasets
Summary

Instruction

Back to the countries dataset. Let's create a new column and see how mutate() and if_else() work together.

Suppose we want a variable stating if a country is in the Americas. We'd create it like this:

countries %>%
  mutate(
    america = if_else(continent == "North America" | continent == "South America",
    TRUE, FALSE))

Notice that if_else() is checking every row to find either North America or South America. Either one will return TRUE.

Exercise

Create a new column named urban_country. Its value will be TRUE if a country's urban population is greater than or equal to 65% and FALSE otherwise. Use mutate() and if_else(). Use the urban_pop_pc column to check if the urban population is greater than or equal to 65 percent.

Stuck? Here's a hint!

The mutate condition will be:

if_else(urban_pop_pc >= 65, TRUE, FALSE)

Type:

countries %>%
  mutate(urban_country = if_else(urban_pop_pc >= 65, TRUE, FALSE))