-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy path299_assignment_1_week2.Rmd
More file actions
141 lines (69 loc) · 5.56 KB
/
Copy path299_assignment_1_week2.Rmd
File metadata and controls
141 lines (69 loc) · 5.56 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<!-- This file by Martin Monkman
is licensed under a Creative Commons Attribution 4.0 International License
https://creativecommons.org/licenses/by/4.0/ -->
```{r echo = FALSE}
library(knitr)
opts_chunk$set(message = FALSE, warning = FALSE, cache = TRUE)
options(width = 100, dplyr.width = 100)
```
# Assignment 1 - Unit 2 - data wrangling & import {#assignment2}
## Setup {-}
This chunk of R code loads the packages that we will be using.
```{r packages_299}
library(tidyverse) # functions using {tidyr}, {dplyr}, and {readr}
library(readxl) # to read Excel files
library(janitor) # for data cleaning
```
## YOUR NAME HERE {-}
## Introduction {-}
For this homework assignment, please write your answer for each question after the question text but before the line break before the next one.
In some cases, you will have to insert R code chunks, and run them to ensure that you've got the right result.
Use all of the R Markdown formatting you want! Bullets, bold text, etc. is welcome. And don't forget to consider changes to the YAML.
Once you have finished your assignment, create an HTML document by "knitting" the document using either the "Preview" or "Knit" button in the top left of the script window frame.
## Questions {-}
### 1. Religion and income data (USA) {-}
(2 marks)
The table `relig_income` is in {tidyr}. Load this table, and take a look at it. You can use `View(relig_income)` to see the table, and to understand the contents of the table you can use the Help function `?tidyr::relig_income` (enter that code in the console).
```{r}
head(tidyr::relig_income)
```
a. What tidy principle(s) does this table violate?
b. Using the functions in {tidyr}, arrange this table in such a way that it is no longer untidy.
### 2. Importing data: csv {-}
#### 2.a Read the data {-}
(6 marks)
**NOTE:**
Variable names with spaces and special symbols are a challenge to work with. They require
that you put the name inside a pair of back-ticks. For example, in the dataset below, a variable called `Court Region` _must_ be inside the back-ticks.
* Remember that you have the option to use the `clean_names()` function in the {janitor} package to resolve this!
* An overview of {janitor} functions is here: https://cran.r-project.org/web/packages/janitor/vignettes/janitor.html
Comma delimited files are one of the most common formats used for flat files. The tidyverse has a package, {readr}, which will automatically load with the function `library(tidyverse)`.
* The reference page for {readr} is here: https://readr.tidyverse.org/index.html
i. Read in the file "cy-concluded-cases-5-yr-regional-report-dashboard-2014-to-2018.csv", which has the number of concluded court cases in B.C. by a variety of dimensions.^[Data source: British Columbia Data Catalogue, [Courts - Concluded Court Cases - 5 Yr Location Report - Dashboard - 2014 to 2018](https://catalogue.data.gov.bc.ca/dataset/courts-concluded-court-cases-5-yr-location-report-dashboard-2014-to-2018). Licensed under Open Government Licence - British Columbia.]
Assign the contents of the file to a named object (remember, name things in a descriptive way!)
ii. Read the file again, but specify that the region code should be a character.
#### 2.b Analysis {-}
c. Now you've read the contents of the file, use your data manipulation skills to create a table that shows the total number of cases that were completed in each court region in 2018. Sort the table so the busiest regions are at the top.
d. Create a table showing the number of cases by court class completed in each region during 2018, with the region as the rows and the court class as the columns.
### 3. Importing data: Excel {-}
#### 3.a Read the data {-}
(2 marks)
Use the {readxl} package to read in the file "bcmetal.xlsx"
This data is published by the Province of British Columbia, and shows the production of metals from mines in British Columbia from 1888 to 1988. ^[The website source for the data is here: https://catalogue.data.gov.bc.ca/dataset/bc-metal-historical-metal-production-1888-1988/resource/69344bab-359e-4cfe-a040-253ae96288ca]
There are three sheets in the file. The sheet we are interested in is BCMETAL_PROD, metal production table for 1452 metal mines operating within the province between 1888 and 1988.
* The reference page for {readxl} is here: https://readxl.tidyverse.org/
i. What are the names of the sheets in this file?
ii. Read in the contents of the "BCMETAL_PROD" sheet.
iii. Reading the file without any additional arguments will generate some warnings.
* The first warning is
> _Warning: Expecting numeric in G8425 / R8425C7: got 'TOTALS:_
The way to interpret this (and all the other warnings) is that R was busy reading the file, and got some unexpected values. Specifically in this case, what is cell G8425 in the Excel sheet (another way to express this location is row (R) 8425 and column (C) 7 is not of the type that was expected. R was busy reading the 7th column, and for 8424 rows found numbers--but in row 8425 it found a character, "TOTAL:"
The problem to solve: read only those rows that have valid data in them.
Change the options for your read function to address any issues you identified in 3.a.ii
#### 3.b Analysis {-}
(6 marks)
Use your data manipulation skills to answer the following, as separate calculations:
i. Based on the data here, in what year was the most gold mined in British Columbia?
ii. What B.C. mine holds the record for the most silver extracted in B.C. through the entire life of the mine?
iii. Does that mine hold the record for the total silver mined during a single year in B.C.?
-30-