```r
library(tidyverse)
<!-- rnb-source-end -->
<!-- rnb-chunk-end -->
<!-- rnb-text-begin -->
#### **1.Using prose, describe how the variables and observations are organized in each of the sample tables ?**
In table1, Each row represents a (country, year) combination. The columns cases and population contain the values for those variables.
<!-- rnb-text-end -->
<!-- rnb-chunk-begin -->
<!-- rnb-source-begin eyJkYXRhIjoiYGBgclxuYGBgclxudGFibGUxO1xuXG5cbmBgYFxuYGBgIn0= -->
```r
```r
table1;
<!-- rnb-source-end -->
<!-- rnb-chunk-end -->
<!-- rnb-text-begin -->
In table2, each row represents a (country, year, variable) combination. The column count contains the values of variables cases and population in separate rows.
<!-- rnb-text-end -->
<!-- rnb-chunk-begin -->
<!-- rnb-source-begin eyJkYXRhIjoiYGBgclxuYGBgclxudGFibGUyO1xuYGBgXG5gYGAifQ== -->
```r
```r
table2;
<!-- rnb-source-end -->
<!-- rnb-chunk-end -->
<!-- rnb-text-begin -->
In table3, each row represents a (country, year) combination. The column rate provides the values of both cases and population in a string formatted like cases / population .
<!-- rnb-text-end -->
<!-- rnb-chunk-begin -->
<!-- rnb-source-begin eyJkYXRhIjoiYGBgclxuYGBgclxudGFibGUzO1xuYGBgXG5gYGAifQ== -->
```r
```r
table3;
<!-- rnb-source-end -->
<!-- rnb-chunk-end -->
<!-- rnb-text-begin -->
Table 4 is split into two tables, one table for each variable. The table table4a contains the values of cases and table4b contains the values of population. Within each table, each row represents a country, each column represents a year, and the cells are the value of the tableโs variable for that country and year.
<!-- rnb-text-end -->
<!-- rnb-chunk-begin -->
<!-- rnb-source-begin eyJkYXRhIjoiYGBgclxuYGBgclxudGFibGU0YTtcbmBgYFxuYGBgIn0= -->
```r
```r
table4a;
<!-- rnb-source-end -->
<!-- rnb-chunk-end -->
<!-- rnb-text-begin -->
<!-- rnb-text-end -->
<!-- rnb-chunk-begin -->
<!-- rnb-source-begin eyJkYXRhIjoiYGBgclxuYGBgclxudGFibGU0YjtcbmBgYFxuYGBgIn0= -->
```r
```r
table4b;
<!-- rnb-source-end -->
<!-- rnb-chunk-end -->
<!-- rnb-text-begin -->
<!-- rnb-text-end -->
<!-- rnb-chunk-begin -->
<!-- rnb-source-begin eyJkYXRhIjoiYGBgclxuYGBgclxudGFibGU1O1xuYGBgXG5gYGAifQ== -->
```r
```r
table5;
<!-- rnb-source-end -->
<!-- rnb-chunk-end -->
<!-- rnb-text-begin -->
**Exercise 12.2.2**
#### **2.Compute the rate for table2, and table4a + table4b. You will need to perform four operations:
*Extract the number of TB cases per country per year.
*Extract the matching population per country per year.
*Divide cases by population, and multiply by 10000.
*Store back in the appropriate place.
*Which representation is easiest to work with? Which is hardest? Why?**
<!-- rnb-text-end -->
<!-- rnb-chunk-begin -->
<!-- rnb-source-begin eyJkYXRhIjoiYGBgclxuYGBgclxudDJfY2FzZXMgPC0gZmlsdGVyKHRhYmxlMiwgdHlwZSA9PSBcXGNhc2VzXFwpICU+JVxuICByZW5hbWUoY2FzZXMgPSBjb3VudCkgJT4lXG4gIGFycmFuZ2UoY291bnRyeSwgeWVhcilcbnQyX3BvcHVsYXRpb24gPC0gZmlsdGVyKHRhYmxlMiwgdHlwZSA9PSBcXHBvcHVsYXRpb25cXCkgJT4lXG4gIHJlbmFtZShwb3B1bGF0aW9uID0gY291bnQpICU+JVxuICBhcnJhbmdlKGNvdW50cnksIHllYXIpXG5cbmBgYFxuYGBgIn0= -->
```r
```r
t2_cases <- filter(table2, type == \cases\) %>%
rename(cases = count) %>%
arrange(country, year)
t2_population <- filter(table2, type == \population\) %>%
rename(population = count) %>%
arrange(country, year)
<!-- rnb-source-end -->
<!-- rnb-chunk-end -->
<!-- rnb-text-begin -->
<!-- rnb-text-end -->
<!-- rnb-chunk-begin -->
<!-- rnb-source-begin eyJkYXRhIjoiYGBgclxuYGBgclxudDJfY2FzZXNfcGVyX2NhcCA8LSB0aWJibGUoXG4gIHllYXIgPSB0Ml9jYXNlcyR5ZWFyLFxuICBjb3VudHJ5ID0gdDJfY2FzZXMkY291bnRyeSxcbiAgY2FzZXMgPSB0Ml9jYXNlcyRjYXNlcyxcbiAgcG9wdWxhdGlvbiA9IHQyX3BvcHVsYXRpb24kcG9wdWxhdGlvblxuKSAlPiVcbiAgbXV0YXRlKGNhc2VzX3Blcl9jYXAgPSAoY2FzZXMgLyBwb3B1bGF0aW9uKSAqIDEwMDAwKSAlPiVcbiAgc2VsZWN0KGNvdW50cnksIHllYXIsIGNhc2VzX3Blcl9jYXApXG5cbmBgYFxuYGBgIn0= -->
```r
```r
t2_cases_per_cap <- tibble(
year = t2_cases$year,
country = t2_cases$country,
cases = t2_cases$cases,
population = t2_population$population
) %>%
mutate(cases_per_cap = (cases / population) * 10000) %>%
select(country, year, cases_per_cap)
<!-- rnb-source-end -->
<!-- rnb-chunk-end -->
<!-- rnb-text-begin -->
<!-- rnb-text-end -->
<!-- rnb-chunk-begin -->
<!-- rnb-source-begin eyJkYXRhIjoiYGBgclxuYGBgclxudDJfY2FzZXNfcGVyX2NhcCA8LSB0Ml9jYXNlc19wZXJfY2FwICU+JVxuICBtdXRhdGUodHlwZSA9IFxcY2FzZXNfcGVyX2NhcFxcKSAlPiVcbiAgcmVuYW1lKGNvdW50ID0gY2FzZXNfcGVyX2NhcClcblxuYGBgXG5gYGAifQ== -->
```r
```r
t2_cases_per_cap <- t2_cases_per_cap %>%
mutate(type = \cases_per_cap\) %>%
rename(count = cases_per_cap)
<!-- rnb-source-end -->
<!-- rnb-chunk-end -->
<!-- rnb-text-begin -->
<!-- rnb-text-end -->
<!-- rnb-chunk-begin -->
<!-- rnb-source-begin eyJkYXRhIjoiYGBgclxuYGBgclxuYmluZF9yb3dzKHRhYmxlMiwgdDJfY2FzZXNfcGVyX2NhcCkgJT4lXG4gIGFycmFuZ2UoY291bnRyeSwgeWVhciwgdHlwZSwgY291bnQpXG5cbmBgYFxuYGBgIn0= -->
```r
```r
bind_rows(table2, t2_cases_per_cap) %>%
arrange(country, year, type, count)
<!-- rnb-source-end -->
<!-- rnb-chunk-end -->
<!-- rnb-text-begin -->
<!-- rnb-text-end -->
<!-- rnb-chunk-begin -->
<!-- rnb-source-begin eyJkYXRhIjoiYGBgclxuYGBgclxudGFibGU0YyA8LVxuICB0aWJibGUoXG4gICAgY291bnRyeSA9IHRhYmxlNGEkY291bnRyeSxcbiAgICBgMTk5OWAgPSB0YWJsZTRhW1tcXDE5OTlcXF1dIC8gdGFibGU0YltbXFwxOTk5XFxdXSAqIDEwMDAwLFxuICAgIGAyMDAwYCA9IHRhYmxlNGFbW1xcMjAwMFxcXV0gLyB0YWJsZTRiW1tcXDIwMDBcXF1dICogMTAwMDBcbiAgKVxudGFibGU0Y1xuYGBgXG5gYGAifQ== -->
```r
```r
table4c <-
tibble(
country = table4a$country,
`1999` = table4a[[\1999\]] / table4b[[\1999\]] * 10000,
`2000` = table4a[[\2000\]] / table4b[[\2000\]] * 10000
)
table4c
<!-- rnb-source-end -->
<!-- rnb-chunk-end -->
<!-- rnb-text-begin -->
***Exercise 12.2.3***
**3.Recreate the plot showing change in cases over time using table2 instead of table1. What do you need to do first?**
<!-- rnb-text-end -->
<!-- rnb-chunk-begin -->
<!-- rnb-source-begin eyJkYXRhIjoiYGBgclxuYGBgclxuXG50YWJsZTIgJT4lXG4gIGZpbHRlcih0eXBlID09IFxcY2FzZXNcXCkgJT4lXG4gIGdncGxvdChhZXMoeWVhciwgY291bnQpKSArXG4gIGdlb21fbGluZShhZXMoZ3JvdXAgPSBjb3VudHJ5KSwgY29sb3VyID0gXFxncmV5NTBcXCkgK1xuICBnZW9tX3BvaW50KGFlcyhjb2xvdXIgPSBjb3VudHJ5KSkgK1xuICBzY2FsZV94X2NvbnRpbnVvdXMoYnJlYWtzID0gdW5pcXVlKHRhYmxlMiR5ZWFyKSkgK1xuICB5bGFiKFxcY2FzZXNcXClcblxuXG5gYGBcbmBgYCJ9 -->
```r
```r
table2 %>%
filter(type == \cases\) %>%
ggplot(aes(year, count)) +
geom_line(aes(group = country), colour = \grey50\) +
geom_point(aes(colour = country)) +
scale_x_continuous(breaks = unique(table2$year)) +
ylab(\cases\)
```