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.
Practice creating and printing objects:
Create an object called ‘x’ and store the number 42 in it
Create an object called ‘y’ and store the text “hello” in it
Print both objects
Check if ‘x’ is numeric using is.numeric()
Check if ‘y’ is numeric using is.numeric()
Hint: Remember how we used the arrow (<-) for assignment and print() function
# Create and store the number 42
x <- 42
# Create and store the text "hello"
y <- "hello"
# Print both objects
print(x)
## [1] 42
## [1] "hello"
## [1] TRUE
## [1] FALSE
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
## ── 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.
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
# 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
Create a new dataset that:
Starts with data from 2007
Creates a new column that converts population to millions
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
# 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
Count how many countries are in each continent in the year 2007.
Hint: Remember how we used count() with continent