Practice Exercises (to leverage for weekly diary!)

Here are 5 exercises to help you practice what we covered today. These build on each other and follow the same order as our tutorial.

Exercise 1: Basic Objects

Practice creating and printing objects:

  1. Create an object called ‘x’ and store the number 42 in it

  2. Create an object called ‘y’ and store the text “hello” in it

  3. Print both objects

  4. Check if ‘x’ is numeric using is.numeric()

  5. Check if ‘y’ is numeric using is.numeric()

Hint: Remember how we used the arrow (<-) for assignment and print() function

Exercise 1: Solution

# Create and store the number 42
x <- 42

# Create and store the text "hello"
y <- "hello"

# Print both objects
print(x)
## [1] 42
print(y)
## [1] "hello"
# Check if numeric
is.numeric(x)  # Will return TRUE
## [1] TRUE
is.numeric(y)  # Will return FALSE
## [1] FALSE

Exercise 2: Variable Selection

Using select(), create a new dataset that includes:

  • country

  • year

  • population

  • GDP per capita

Hint: This is just like what we did with life expectancy, but choosing different variables

Exercise 2: Solution

library(gapminder)
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.5
## ✔ forcats   1.0.0     ✔ stringr   1.5.1
## ✔ ggplot2   3.5.1     ✔ tibble    3.2.1
## ✔ lubridate 1.9.3     ✔ tidyr     1.3.1
## ✔ purrr     1.0.2     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
# Select specific variables from gapminder
df <- gapminder %>%
  select(country, year, pop, gdpPercap)

# Check the result
head(df)
## # A tibble: 6 × 4
##   country      year      pop gdpPercap
##   <fct>       <int>    <int>     <dbl>
## 1 Afghanistan  1952  8425333      779.
## 2 Afghanistan  1957  9240934      821.
## 3 Afghanistan  1962 10267083      853.
## 4 Afghanistan  1967 11537966      836.
## 5 Afghanistan  1972 13079460      740.
## 6 Afghanistan  1977 14880372      786.

Exercise 3: Filtering Data

Filter the gapminder dataset to show:

  • Only data from the year 1997

  • Only countries from Africa

  • Only these two variables: country and population

Hint: Remember how we filtered for 2007 and Europe? Just change those values

Exercise 3: Solution

# Filter for African countries in 1997
df <- gapminder %>%
  filter(
    year == 1997,
    continent == "Africa"
  ) %>%
  select(country, pop)

# Check the result
df
## # A tibble: 52 × 2
##    country                       pop
##    <fct>                       <int>
##  1 Algeria                  29072015
##  2 Angola                    9875024
##  3 Benin                     6066080
##  4 Botswana                  1536536
##  5 Burkina Faso             10352843
##  6 Burundi                   6121610
##  7 Cameroon                 14195809
##  8 Central African Republic  3696513
##  9 Chad                      7562011
## 10 Comoros                    527982
## # ℹ 42 more rows

Exercise 4: Creating New Variables

Create a new dataset that:

  1. Starts with data from 2007

  2. Creates a new column that converts population to millions

  3. Shows only: country, continent, population, and your new population in millions column

Hint: Look at how we converted GDP to billions, but use different math for millions

Exercise 4: Solution

# Create population in millions for 2007
df <- gapminder %>%
  filter(year == 2007) %>%
  mutate(
    pop_millions = pop / 1000000
  ) %>%
  select(country, continent, pop, pop_millions)

# Check the result
head(df)
## # A tibble: 6 × 4
##   country     continent      pop pop_millions
##   <fct>       <fct>        <int>        <dbl>
## 1 Afghanistan Asia      31889923        31.9 
## 2 Albania     Europe     3600523         3.60
## 3 Algeria     Africa    33333216        33.3 
## 4 Angola      Africa    12420476        12.4 
## 5 Argentina   Americas  40301927        40.3 
## 6 Australia   Oceania   20434176        20.4

Exercise 5: Basic Counting

Count how many countries are in each continent in the year 2007.

Hint: Remember how we used count() with continent

Exercise 5: Solution

# Count countries per continent in 2007
countC <- gapminder %>%
  filter(year == 2007) %>%
  count(continent, sort = TRUE)

countC
## # A tibble: 5 × 2
##   continent     n
##   <fct>     <int>
## 1 Africa       52
## 2 Asia         33
## 3 Europe       30
## 4 Americas     25
## 5 Oceania       2