For each exercise below, show code. Once you’ve completed things, don’t forget to input everything into the quiz on Canvas and to upload this document (knitted version please!) at the end of the quiz. A few tips:
install.packages() and load them using
library().gapminder
dataset?library(gapminder)
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.3 ✔ readr 2.1.4
## ✔ forcats 1.0.0 ✔ stringr 1.5.0
## ✔ ggplot2 3.4.3 ✔ tibble 3.2.1
## ✔ lubridate 1.9.2 ✔ tidyr 1.3.0
## ✔ 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
data("gapminder")
unique(gapminder)
## # A tibble: 1,704 × 6
## country continent year lifeExp pop gdpPercap
## <fct> <fct> <int> <dbl> <int> <dbl>
## 1 Afghanistan Asia 1952 28.8 8425333 779.
## 2 Afghanistan Asia 1957 30.3 9240934 821.
## 3 Afghanistan Asia 1962 32.0 10267083 853.
## 4 Afghanistan Asia 1967 34.0 11537966 836.
## 5 Afghanistan Asia 1972 36.1 13079460 740.
## 6 Afghanistan Asia 1977 38.4 14880372 786.
## 7 Afghanistan Asia 1982 39.9 12881816 978.
## 8 Afghanistan Asia 1987 40.8 13867957 852.
## 9 Afghanistan Asia 1992 41.7 16317921 649.
## 10 Afghanistan Asia 1997 41.8 22227415 635.
## # ℹ 1,694 more rows
# There are 1704 observations.
# The unique command allows us to simply see the total number of rows within a data set.
class() of each variable in the
gapminder dataset. Describe the the difference
between"numeric" and "integer". What’s the
class of year?# Your code here!
class(gapminder$country)
## [1] "factor"
class(gapminder$continent)
## [1] "factor"
class(gapminder$year)
## [1] "integer"
class(gapminder$lifeExp)
## [1] "numeric"
class(gapminder$pop)
## [1] "integer"
class(gapminder$gdpPercap)
## [1] "numeric"
# Numeric classes represent data which can have non-whole number values (decimals). Integers represent data which can only be whole numbers. For instance, population data would be an integer as it counts each person (e.g. there cannot be population of 100.5 people). Oppositely, life expectancy data would be numeric as that data can be represented as partial years. With this said, the Year variable will be a integer as the data represents an entire year, not a portion of one.
length() function.length(unique(gapminder$country))
## [1] 142
# There are 142 unqiue countries.
# I started by using the unique function on the countries variable. I then used the length function to calculate the total number of unique values this gave us.
filter().filter(gapminder, country == "Oman" & year == 2007)
## # A tibble: 1 × 6
## country continent year lifeExp pop gdpPercap
## <fct> <fct> <int> <dbl> <int> <dbl>
## 1 Oman Asia 2007 75.6 3204897 22316.
# For this data set, the population of Oman in 2007 was 3,204,897.
# Using the filter function, we could identify the data fitting a series of criteria. Here, we specify the country and year.
filter() and arrange().yr07 <- filter(gapminder, year == 2007)
head(arrange(yr07, desc(gdpPercap)),5)
## # A tibble: 5 × 6
## country continent year lifeExp pop gdpPercap
## <fct> <fct> <int> <dbl> <int> <dbl>
## 1 Norway Europe 2007 80.2 4627926 49357.
## 2 Kuwait Asia 2007 77.6 2505559 47307.
## 3 Singapore Asia 2007 80.0 4553009 47143.
## 4 United States Americas 2007 78.2 301139947 42952.
## 5 Ireland Europe 2007 78.9 4109086 40676.
# Norway, Kuwait, Singapore, United States, Ireland.
# I started by creating a new vector (yr07) to simply show just the data from 2007. I then used the "head...5" command to specify that I only wanted the top 5 countries. Lastly, I used the arrange function to sort the yr07 data based on the GDP.
group_by() and
summarize()!)gapminder %>%
group_by(country)%>%
select(year, lifeExp) %>%
summarize(avg_life = mean(lifeExp))%>%
arrange(desc(-avg_life))%>%
head(n=5)
## Adding missing grouping variables: `country`
## # A tibble: 5 × 2
## country avg_life
## <fct> <dbl>
## 1 Sierra Leone 36.8
## 2 Afghanistan 37.5
## 3 Angola 37.9
## 4 Guinea-Bissau 39.2
## 5 Mozambique 40.4
# Sierra Leone, Afghanistan, Angola, Guinea-Bissau, and Mozambique have the lowest life expectancy.
# Building off the previous question, I started by grouping the data by country. I then selected the year and life expectancy then calculated the average life expectancy of each group of countries. After this, I used the arrange command to put the countries in order from lowest to highest lifeExp, then specified that I only wanted to see the lowest 5 countries.
head(arrange(yr07, desc(pop)), 3)
## # A tibble: 3 × 6
## country continent year lifeExp pop gdpPercap
## <fct> <fct> <int> <dbl> <int> <dbl>
## 1 China Asia 2007 73.0 1318683096 4959.
## 2 India Asia 2007 64.7 1110396331 2452.
## 3 United States Americas 2007 78.2 301139947 42952.
# China, India, and the United states were the most populated countries in 2007.
# This one is very similar to question 5. I just used the arrange command to order the population data from 2007 then used the "head...3" command to show the top 3 countries.
africa where
observations located in the continent of Africa are coded as “Africa”
and those not located in Africa as “Not Africa.” Use dplyr
to compute the average life expectancy and GDP per capita in countries
located within Africa and outside of Africa in 2007. (2 points)Africa <-gapminder %>%
mutate(Africa = ifelse(continent == "Africa", "Africa", "Not Africa"))
Africa %>%
filter(year == 2007) %>%
group_by(Africa) %>%
select(year, lifeExp, gdpPercap) %>%
summarize(avg_life = mean(lifeExp), mean_gpd = mean(gdpPercap))
## Adding missing grouping variables: `Africa`
## # A tibble: 2 × 3
## Africa avg_life mean_gpd
## <chr> <dbl> <dbl>
## 1 Africa 54.8 3089.
## 2 Not Africa 74.1 16644.
# The Life expectancy in the continent of Africa in 2007 was 54.8 years old.
# Using the mutate command, I added a new variable and assigned the values as "Africa", "Africa", and "Not Africa". I then filtered the year and grouped the new variable. Lastly, I selected the year, lifeexp, and GDP columns to then summarize/average out. This then gave us averaged data across the values in the Africa variable.