-
Notifications
You must be signed in to change notification settings - Fork 0
/
hw01_gapminder.Rmd
57 lines (44 loc) · 1.41 KB
/
hw01_gapminder.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
---
title: "HW01 Gapminder"
author: "85868164 Roger Yu-Hsiang Lo"
date: '2018-09-15'
output: github_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## Set-up
* Install (if not yet) and load the `gapminder` R package.
```{r}
#install.packages('gapminder')
library(gapminder)
```
* Some sanity check to make sure the data are loaded properly.
```{r}
head(gapminder)
```
## Basic exploration of the data
* The number of the countries included in the data set.
```{r}
length(unique(gapminder$country))
```
* The distribution of the life expectancy across different continents in the year of 2007.
```{r}
# Create another data frame with only entries from 2007
gapminder_2007 = gapminder[gapminder$year == 2007,]
# Sanity check
head(gapminder_2007)
# Draw a boxplot to show the distribution
boxplot(lifeExp ~ continent, data = gapminder_2007)
```
* Draw a scatter plot to see if there is correlation between GDP per capita and life expectancy for the 2007 data.
```{r}
plot(x = gapminder_2007$gdpPercap, y = gapminder_2007$lifeExp, xlab = 'GDP per capita', ylab = 'Life expectancy')
```
* How life expectancy in Taiwan changed over years.
```{r}
# Create a new data frame that includes only the data from Taiwan
gapminder_taiwan = gapminder[gapminder$country == 'Taiwan',]
# Draw a scatter plot
plot(x = gapminder_taiwan$year, y = gapminder_taiwan$lifeExp, xlab = 'Year', ylab = 'Life expectancy')
```