library(rmarkdown)
library(gapminder)
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(ggplot2)
summarize to find the median life expectancy
gapminder%>%
summarise(medianLifeExp=median(lifeExp))
## # A tibble: 1 × 1
## medianLifeExp
## <dbl>
## 1 60.7
filter for 1957 then summarize the median life expectancy
gapminder%>%
filter(year==1957)%>%
summarize(medianLifeExp=median(lifeExp))
## # A tibble: 1 × 1
## medianLifeExp
## <dbl>
## 1 48.4
filter for 1957 then summarize the median life expectancy and the maximum GDP per capita
gapminder%>%
filter(year==1957)%>%
summarize(medianLifeExp=median(lifeExp),
maxGdpPercap=max(gdpPercap))
## # A tibble: 1 × 2
## medianLifeExp maxGdpPercap
## <dbl> <dbl>
## 1 48.4 113523.
find median life expectancy and maximum GDP per capita in each year
gapminder%>%
group_by(year)%>%
summarize(medianLifeExp=median(lifeExp),
maxGdpPercap=max(gdpPercap))
## # A tibble: 12 × 3
## year medianLifeExp maxGdpPercap
## <int> <dbl> <dbl>
## 1 1952 45.1 108382.
## 2 1957 48.4 113523.
## 3 1962 50.9 95458.
## 4 1967 53.8 80895.
## 5 1972 56.5 109348.
## 6 1977 59.7 59265.
## 7 1982 62.4 33693.
## 8 1987 65.8 31541.
## 9 1992 67.7 34933.
## 10 1997 69.4 41283.
## 11 2002 70.8 44684.
## 12 2007 71.9 49357.
find median life expectancy and maximum GDP per capita in each continent in 1957
gapminder%>%
group_by(continent)%>%
summarize(medianLifeExp=median(lifeExp),
maxGdpPercap=max(gdpPercap))
## # A tibble: 5 × 3
## continent medianLifeExp maxGdpPercap
## <fct> <dbl> <dbl>
## 1 Africa 47.8 21951.
## 2 Americas 67.0 42952.
## 3 Asia 61.8 113523.
## 4 Europe 72.2 49357.
## 5 Oceania 73.7 34435.
find median life expectancy and maximum GDP per capita in each continent/year combination
gapminder%>%
group_by(continent,year)%>%
summarize(medianLifeExp=median(lifeExp),
maxGdpPercap=max(gdpPercap))
## `summarise()` has grouped output by 'continent'. You can override using the
## `.groups` argument.
## # A tibble: 60 × 4
## # Groups: continent [5]
## continent year medianLifeExp maxGdpPercap
## <fct> <int> <dbl> <dbl>
## 1 Africa 1952 38.8 4725.
## 2 Africa 1957 40.6 5487.
## 3 Africa 1962 42.6 6757.
## 4 Africa 1967 44.7 18773.
## 5 Africa 1972 47.0 21011.
## 6 Africa 1977 49.3 21951.
## 7 Africa 1982 50.8 17364.
## 8 Africa 1987 51.6 11864.
## 9 Africa 1992 52.4 13522.
## 10 Africa 1997 52.8 14723.
## # ℹ 50 more rows
by_year<-gapminder%>%
group_by(year)%>%
summarize(medianLifeExp=median(lifeExp),
maxGdpPercap=max(gdpPercap))
create a scatter plot showing the change in medianLifeExp over time
ggplot(by_year,aes(x=year,y=medianLifeExp))+
geom_point()+
expand_limits(y=0)
summarise medianGdpPercap within each continent within each year: by_year_continent
by_year_continent<-gapminder%>%
group_by(continent,year)%>%
summarize(medianGdpPercap=median(gdpPercap))
## `summarise()` has grouped output by 'continent'. You can override using the
## `.groups` argument.
plot the change in medianGdpPercap in each continent over time
ggplot(by_year_continent,aes(x=year,y=medianGdpPercap,color=continent))+
geom_point()+
expand_limits(y=0)
summarise the median GDP and median life expectancy per continent in 2007
by_continent_2007<-gapminder %>%
filter(year==2007) %>%
group_by(continent) %>%
summarize(medianGdpPercap=median(gdpPercap),
medianLifeExp=median(lifeExp))
ggplot(by_continent_2007,aes(x=medianGdpPercap,y=medianLifeExp,color=continent))+
geom_point()