Instructions

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:


Q1: How many observations are there in the gapminder dataset?

library(tidyverse)
library(gapminder)
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 1,704 observations in the 'gapminder' dataset. 
#Unique(gapminder) shows how many rows and columns are in a dataset.

Q2: Show the class() of each variable in the gapminder dataset. Describe the the difference between"numeric" and "integer". What’s the class of year?

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"
#A numeric value can either be a whole number or a number with a decimal, while an integer value can only be a whole number. The class 'year' is integer. 

Q3: How many unique countries are in the dataset? Hint: Look at the length() function.

length(unique(gapminder$country))
## [1] 142
#There are 142 unqiue countries in the dataset. Length and unique tool specifies how many different values are in a column

Q4: What was the population of Oman in 2007? Hint: Use 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.
#The population of Oman in 2007 was 3,204,897. Used the filter tool to specify which country and year within the gapminder dataset that I was interested in. 

Q5: Which 5 countries have the highest GDP per capita in 2007? Hint: Use filter() and arrange().

year2007 = filter(gapminder, year == 2007)
head(arrange(year2007, 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, USA, and Ireland had the highest GDP per capita in 2007. The first line of code create a new vector with all data just from the year 2007. In the second line of code, I used the head funtion with the #5 to specify that I wanted to single out the 5 countries the with highest gdp. The arrange function orders the rows in descending order

Q6: Which 5 countries have the lowest average life expectancy over the period from 1952 to 2007? (Hint: 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 had the lowest life expectancy over the specifed time frame. Grouped the data by country and selected for year and life expectancy. Used summarize function to calcualte average life expectancy for countries. Arranged countries in order from lowest life expectancy, and used head function to only show top 5 countries with lowest expectancy. 

Q7: List the top three countries in terms of population in 2007.

head(arrange(year2007, 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.
#In 2007, China, India, and the US had the highest population. Used head and #3 function to show only top 3 countries and arrange in descending order. 

Q8: Create a new variable called 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 average life expectancy for African countries in 2007 is 54.8 years, and in non-African countries is 74.1 years. The mean GDP is African countries is 3089, and in non-African countries is 16,643.8. I created a new vector for Africa and used the mutate ifelse function to determine if each country was true or false, or Africa or not Africa. Then I filtered the data for the year 2007, life expectancy, and gdp. I used summarize tool to calcualte averages.