Lab 5
1. Gapminder

The goal of the gapminder dataset is to explore the relationship between GDP, population, and life expectancy.
-
Load the
tidyversepackage and thegapminderdataset from thegapminderpackage. -
What are the mean life expectancies and populations seen by year for each continent?
-
Using
across(), can you get the min, mean, median, and max life expectancy for each country? -
Building off of your last answer, can you use
rowwise()andmutate()to calculate the range for each country, subtracting lifeExp_min from lifeExp_max? Arrange descending by your new lifeExp_range variable.- Yes, there is a
range()function you could have used in youracross()function call. While you can validate your max-min subtraction with that, I’d like you to practicerowwise()here!
- Yes, there is a
-
How many countries are there on each continent, excluding duplicates?
2. Diamonds
The goal of the diamonds dataset is to see which characteristics are most influential on price.
-
Use
data()to load thediamondsdataset from ggplot2. -
According to the diamond documentation, the variable
depthis calculated using the formula . Implement this formula usingrowwise()andmutate(). Name your new variabledepth_check. -
Using
case_when(), create an indicator variableperfectof all diamonds that meet the following conditions:- cut is premium or ideal & color is D or E & carat is bigger than 1 & clarity is VVS1 or IF. create a plot with this new variable of carat (x) by price (y), colored by “perfect”. Here’s some code to get your started…
diamonds %>%
mutate(perfect = case_when((cut conditions) &
(color conditions) &
(carat conditions) &
(clarity conditions) ~ "Perfect",",
TRUE ~ "Not Perfect")) %>%
ggplot()