Skip to content

Commit 3c7b708

Browse files
committed
add skeleton of R section
1 parent ef5dfac commit 3c7b708

2 files changed

Lines changed: 105 additions & 3 deletions

File tree

materials/DESCRIPTION

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ Imports:
5656
quanteda,
5757
readr,
5858
reshape2,
59+
skimr,
5960
udunits2,
6061
uuid,
6162
sf,

materials/sections/data-quality.qmd

Lines changed: 104 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,21 +76,122 @@ At the Arctic Data Center, we have chosen to tackle the first two types of data
7676

7777
When these checks are run, each dataset recieves a report like the below:
7878

79-
![](images/adc-data-quality-overview.png){width="80%" fig-align="center"}
79+
![](images/adc-data-quality-overview.png){fig-align="center"}
8080

8181
Warnings are issued for checks that failed, but are optional. Within each row, output describes the result of the check. For example: Column names in Riverbank_Sediment_ADC.csv are well formed. No non-printable characters detected.
8282

8383
Some output is collapsed for readability:
8484

85-
![](images/adc-data-quality-warning-detail.png){width="80%" fig-align="center"}
85+
![](images/adc-data-quality-warning-detail.png){fig-align="center"}
8686

8787
At the Arctic Data Center we can use these checks to gain insights about our data holdings. TODO: maybe insert figures here?
8888

89-
### Data Quality and You
89+
### Checking Data Quality in R
9090

9191
Poll:
9292

9393
- What are the most common quality issues you see? (free text)
9494
- What kind of data quality checks do you use on your own data? (free text)
9595
- Do you have ideas for checks you think we should add to the ADC quality suite?
9696

97+
Although the Arctic Data Center has implemented data quality checks that are automated, the vast majority of data quality work still falls on researchers doing analysis. In this section we will discuss a few tools that researchers can use in R to do general data quality work.
98+
99+
#### Resolving mysteries with `file`
100+
101+
When a researcher receives a new file to incorporate into an analysis, the first step is to examine the file. One tool already used in the Arctic Data Center data quality suite is the `file` utility in linux/unix systems. This is a command run using the terminal that performs tests on the **content** of a file to determine its type. This is useful for files where the type is unknown, or for files that cannot be read into R for unknown reasons.
102+
103+
In the terminal, run the following:
104+
105+
```
106+
file data/BGchem2008data.csv
107+
```
108+
109+
You should see an output like: `data/BGchem2008data.csv: CSV text`. Which makes sense!
110+
111+
The file command is very helpful at catching things like this, where we have an excel file masquerading as a csv file. You might wind up using this if you try to read in the example file below using `read.csv`, and get output that looks completely garbled.
112+
113+
```
114+
file example-data/my-data.csv
115+
example-data/my-data.csv: Microsoft Excel 2007+
116+
```
117+
118+
Another great use of the file command is for mystery extensions:
119+
120+
```
121+
file example-data/JUSTIN_400__001.DZG
122+
example-data/JUSTIN_400__001.DZG: ASCII text, with CRLF, LF line terminators
123+
```
124+
125+
#### Quick Summaries with `skimr`.
126+
127+
First, we need to install the `skimr` package.
128+
129+
```{r, eval = FALSE}
130+
install.packages("skimr")
131+
```
132+
133+
And load it into our environment, along with `readr`
134+
135+
```{r, warning = FALSE, message = FALSE}
136+
library(skimr)
137+
library(readr)
138+
library(dplyr)
139+
library(lubridate)
140+
```
141+
142+
Now we'll read in a familar file, the `BGChem2008data.csv` from [Craig Tweedie. (2009). North Pole Environmental Observatory Bottle Chemistry. Arctic Data Center. doi:10.18739/A25T3FZ8X.](https://doi.org/doi:10.18739/A25T3FZ8X)
143+
144+
```{r}
145+
bg_chem <- read_csv("data/BGchem2008data.csv")
146+
```
147+
Notice that the `read_csv` call already gives us some information about the dataset here. It tells us what the columns are, and what the column types are. `skimr` can give us even more information that will be helpful.
148+
149+
```{r}
150+
skim(bg_chem)
151+
```
152+
153+
`skim` gives us information about the data frame including basic statistics on the numeric values, missing values, and number of unique values in the character variables.
154+
155+
Helpfully, `skim` can also handle grouped output, if we wanted to look more closely at summaries of oxygen concentaration by `station`, for example, we can run:
156+
157+
```{r}
158+
bg_chem %>%
159+
select(Station, O2) %>%
160+
group_by(Station) %>%
161+
skim()
162+
```
163+
164+
Skim is a great way to get a big picture overview of your data to spot major issues. Does anything stand out here?
165+
166+
-99 (or some repetition of those digits) is a very common missing value code that isn't automatically recognized by R. Let's go back to the `read_csv` call to add it in as an argument.
167+
168+
```{r}
169+
bg_chem <- read_csv("data/BGchem2008data.csv", na = "-99")
170+
```
171+
172+
Another similar package is called `visdat`. It lets you visualize a data frame with its column types, variable names, and missing values.
173+
174+
```{r, eval = FALSE}
175+
install.packages("visdat")
176+
```
177+
178+
```{r}
179+
library(visdat)
180+
```
181+
182+
```{r}
183+
vis_dat(bg_chem) +
184+
theme(axis.text.x = element_text(angle = 45, vjust = 0))
185+
```
186+
187+
Similar to `skimr`, you can also group (in this case facet) the output by a variable.
188+
189+
```{r}
190+
vis_dat(bg_chem, facet = Station) +
191+
theme(axis.text.x = element_text(angle = 45, vjust = 0))
192+
```
193+
194+
This is helpful because it shows us where the missing values are and in which variable.
195+
196+
197+

0 commit comments

Comments
 (0)