12.2.1
1.
Using prose, describe how the variables and observations are organised in each of the sample tables.
Answer:
In table1 each row is a (country, year) with variables cases and population.
table1
In table2, each row is country, year , variable (“cases”, “population”) combination, and there is a count variable with the numeric value of the combination.
In table3, each row is a (country, year) combination with the column rate having the rate of cases to population as a character string in the format “cases/rate”.
Table 4 is split into two tables, one table for each variable: table4a is the table for cases, while table4b is the table for population. Within each table, each row is a country, each column is a year, and the cells are the value of the variable for the table.
table4a
table4b
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?
Answer:
Using some code from Chapter 13. Relational data
Using table4a and table4b
1.
What do the extra and fill arguments do in separate()? Experiment with the various options for the following two toy datasets.
tibble(x = c("a,b,c", "d,e", "f,g,i")) %>%
separate(x, c("one", "two", "three"))
Expected 3 pieces. Missing pieces filled with `NA` in 1 rows [2].
Examples:
tibble(x = c("a,b,c", "d,e,f,g", "h,i,j")) %>%
separate(x, c("one", "two", "three"), extra = "drop")
tibble(x = c("a,b,c", "d,e,f,g", "h,i,j")) %>%
separate(x, c("one", "two", "three"), extra = "merge")
tibble(x = c("a,b,c", "d,e", "f,g,i")) %>%
separate(x, c("one", "two", "three"), fill = "right")
tibble(x = c("a,b,c", "d,e", "f,g,i")) %>%
separate(x, c("one", "two", "three"), fill = "left")
LS0tCnRpdGxlOiAnU3RhdC4gNDUwIFNlY3Rpb24gMSBvciAyOiBIb21ld29yayA4JwpvdXRwdXQ6CiAgd29yZF9kb2N1bWVudDogZGVmYXVsdAogIHBkZl9kb2N1bWVudDogZGVmYXVsdAogIGh0bWxfbm90ZWJvb2s6IGRlZmF1bHQKICBodG1sX2RvY3VtZW50OgogICAgZGZfcHJpbnQ6IHBhZ2VkCi0tLQoKKipQcm9mLiBFcmljIEEuIFN1ZXNzKioKClNvIGhvdyBzaG91bGQgeW91IGNvbXBsZXRlIHlvdXIgaG9t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