Skip to content

Commit ceb1b9e

Browse files
committed
Fixes and expands the data integrity testing examples.
1 parent b4619d0 commit ceb1b9e

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

rr-intro-exercise.Rmd

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ document including this narrative, the R code, and figures should pop up.
1515

1616
The `read.csv` function is used to read the data into R. Note that the argument provided
1717
for this function is the complete path that leads to the dataset from your current
18-
working directory (where this RMarkdown file is located). Also note that this is provided
19-
as a character string, hence in quotation marks.
18+
working directory (where this RMarkdown file is located). Also note that this
19+
is provided as a character string, hence in quotation marks.
2020

2121
```{r}
2222
gap_5060 <- read.csv("data/gapminder-5060.csv")
@@ -58,6 +58,28 @@ ggplot(data = gap_5060_CA, aes(x = year, y = lifeExp)) +
5858
geom_line()
5959
```
6060

61+
### Test data integrity expectations
62+
63+
Life expectancy shouldn't exceed even the most extreme age observed for humans.
64+
```{r, error=TRUE}
65+
if (any(gap_5060$lifeExp > 150)) {
66+
stop("impossible life expectancy")
67+
}
68+
```
69+
70+
The library `testthat` allows us to make this a little more readable:
71+
```{r}
72+
library(testthat)
73+
```
74+
```{r, error=TRUE}
75+
expect_that(any(gap_5060$lifeExp > 150), is_false(),
76+
"one or more life expectancies are improbably high")
77+
```
78+
```{r, error=TRUE}
79+
expect_that(any(gap_5060$pop <= 0), is_false(),
80+
"one or more population sizes are zero or negative")
81+
```
82+
6183
### Task 2: Identify and fix the data error
6284

6385
Something is clearly wrong with this plot! Turns out there's a data error in the data file: life expectancy for Canada in the year 1957 is coded as `999999`, it should actually be `69.96`. Make this correction.

0 commit comments

Comments
 (0)