-
Notifications
You must be signed in to change notification settings - Fork 0
/
130-missing-values.qmd
223 lines (147 loc) · 3.58 KB
/
130-missing-values.qmd
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
---
title: "Missing Values"
subtitle: "How to operate on values that are not known."
author: "Otho Mantegazza _ Dataviz for Scientists _ Part 1.3"
editor_options:
chunk_output_type: console
---
```{r}
#| include: false
library(dplyr)
library(readr)
library(magrittr)
library(tidyr)
library(palmerpenguins)
library(tibble)
library(ggplot2)
library(readr)
library(here)
library(stringr)
library(broom)
theme_update(axis.title = element_text(size = 15),
axis.text = element_text(size = 15),
strip.text = element_text(size = 15))
```
## What is a missing value?
::: {.column width="70%"}
A missing value is a data point that's missing, no one knows what the value was, so you can't conduct any operation on it.
In R missing values are written **NA**.
:::
## Operation on missing values
::: {.column width="70%"}
Most operation on missing values return NAs.
Ask yourself what is one plus "a number that I don't know" (**NA**). The answer is "I don't know" (**NA**).
```{r}
NA + 1
```
```{r}
NA / 4
```
```{r}
NA == 1
```
```{r}
NA == NA
```
:::
## Testing if a value is missing
::: {.column width="70%"}
To test if an element is a missing value we must use the function **is.na()**.
```{r}
1 %>% is.na()
```
```{r}
'hello' %>% is.na()
```
```{r}
TRUE %>% is.na()
```
```{r}
NA %>% is.na()
```
:::
## Missing values in a variable
::: {.column width="70%"}
We can assign missing values to a variable and place them in a vector.
```{r}
missing_value <- NA
```
```{r}
missing_value %>% is.na()
```
:::
## Missing values in a vector
::: {.column width="70%"}
When we use **is.na()** on a vector, it returns a vector of booleans, with **TRUE** in the position where the values are missing.
```{r}
vector_with_na <- c(1, 5, NA, 10)
```
```{r}
vector_with_na %>% is.na()
```
<br>
Remember, we can't use the **==** statement to test if a vector stores NAs.
```{r}
vector_with_na == NA
```
:::
## Missing values in a vector
::: {.column width="70%"}
We can count missing values in a vector with **is.na() %>% sum()**.
```{r}
vector_with_na %>% is.na() %>% sum()
```
:::
## Operating on missing values
::: {.column width="70%"}
Most functions such as **mean()**, **median()**, **sd()** give you the chance to remove missing values with the argument **na.rm = TRUE**.
```{r}
vector_with_na %>% mean()
```
```{r}
vector_with_na %>% mean(na.rm = T)
```
Also, **ggplot**'s function remove NAs automatically for us, if they would hinder computations.
:::
## Data with missing values
::: {.column width="70%"}
Often data have NAs in them, for example the **penguins** dataset does.
To count missing values per each column, use this lines of code:
:::
```{r}
penguins %>%
summarise(
across(
everything(),
~is.na(.) %>% sum()
)
)
```
## Data with missing values
::: {.column width="70%"}
We can also count them stratified by the fixed variables, for example:
:::
```{r}
penguins %>%
group_by(island) %>%
summarise(
across(
everything(),
~is.na(.) %>% sum()
)
)
```
## Strategy for missing values
::: {.column width="70%"}
Often data have missing values.
The most important thing, when you get new data is to figure out how many missing values it contains, and where they are.
Afterward you can decide if you want to remove them, or to impute them.
**Learn more** about missing values at [R4DS](https://r4ds.had.co.nz/transform.html#missing-values).
:::
## Exercise
::: {.column width="70%"}
Identify the columns of the penguins dataset that contain NAs.
Substitute the missing values:
- With 0s.
- With the mean of the column.
:::