-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdealing_with_NAs.Rmd
335 lines (257 loc) · 9.15 KB
/
dealing_with_NAs.Rmd
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
---
title: ""
author: ""
date: ""
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, df_print="info_paged")
```
```{r, echo=FALSE}
info_paged_print <- function(x, options) {
tibble_info <- paste0("<div class=\"tibble-info\">A tibble: ", nrow(x), " x ", ncol(x), "</div>")
group_info <- paste0("<div class=\"group-info\">Groups: ",
paste0(group_vars(x), collapse = ", "),
" [", nrow(group_keys(x)), "]", "</div>")
if (dplyr::is_grouped_df(x)) {
tab_info <- paste0("<div class=\"info\">", tibble_info, " ", group_info, "</div>")
cat(tab_info)
} else {
cat(paste0("<div class=\"info\">", tibble_info, "</div>"))
}
knitr::asis_output(
rmarkdown:::paged_table_html(x, options = attr(x, "options")),
meta = list(
dependencies = rmarkdown:::html_dependency_pagedtable()
)
)
}
knitr::opts_hooks$set(df_print = function(options) {
if (options$df_print == "info_paged") {
options$render = info_paged_print
options$comment = ""
options$results = "asis"
}
options
})
```
```{css, echo=FALSE}
.tibble-info,
.group-info {
display: inline-block;
padding: 15px;
}
.info {
margin-top: 5px;
margin-bottom: 5px;
border: 1px solid #ccc;
border-radius: 4px;
font-weight: 600;
color: #999898;
}
```
```{r, message = FALSE, echo=FALSE}
library(dplyr)
library(readxl)
df <- read_excel(here::here("online_retail_II.xlsx"))
```
# - *introduction*
Missing values in a data frame are coded as NA (not available) and are elements that can prove themselves troublesome, especially as, when involved, they can alter the results of calculations.
```{r, df_print = "paged"}
(x <- c(1, 2, 3))
mean(x)
sum(x)
(xNA <- c(x, NA))
mean(xNA)
sum(xNA)
```
`mean()` and `sum()` have an `na.rm` argument that helps deal with them
```{r, df_print = "paged"}
mean(xNA, na.rm = TRUE)
sum(xNA, na.rm = TRUE)
```
but other functions might not, so it is important to know how to detect and remove or substitute them, hence why I decided to collect and organize in this page procedures possibly already presented in other ones.
# - *column-wise detection*
At first it is useful to know their location, marking with TRUE the columns where they are present and with FALSE otherwise.
```{r}
df %>%
summarise(across(everything(), ~ any(is.na(.x))))
```
Additionally, we may want to know how many of them are there per column,
```{r}
df %>%
summarise(across(everything(), ~ sum(is.na(.x))))
```
and as a percentage over the number of rows.
```{r}
df %>%
summarise(across(everything(), ~ formattable::percent(mean(is.na(.x)))))
```
We can combine those two information in just one output.
```{r}
df %>%
summarise(across(everything(), ~ sum(is.na(.x)))) %>%
tidyr::pivot_longer(everything(), names_to = "Column", values_to = "#_of_NAs") %>%
bind_cols(df %>%
summarise(across(everything(), ~ formattable::percent(mean(is.na(.x))))) %>%
tidyr::pivot_longer(everything(), names_to = "Column", values_to = "%_of_NAs") %>%
select(2))
```
# - *row-wise detection*
Knowing about their presence and number for each row can also be useful.
```{r}
df %>%
mutate(row_with_NAs = as.logical(rowSums(is.na(df))), .before = 1)
df %>%
mutate(`#_NAs` = rowSums(is.na(df)), .before = 1)
```
# - *group-wise detection*
For a grouped data frame we can show, for every column and for groups with NAs, both their total number
```{r}
df %>%
group_by(Country) %>%
summarise(across(everything(), ~ sum(is.na(.x)))) %>%
filter(rowSums(across(Invoice:`Customer ID`)) != 0)
```
and the percentage over the total number of rows for each group.
```{r}
df %>%
group_by(Country) %>%
summarise(across(everything(), ~ formattable::percent(mean(is.na(.x))))) %>%
filter(rowSums(across(Invoice:`Customer ID`)) != 0)
```
We can also concentrate on only one column,
```{r}
df %>%
group_by(Country) %>%
summarise(NAs_in_Customer_ID = any(is.na(`Customer ID`)))
df %>%
group_by(Country) %>%
summarise(`#_of_NAs_in_Customer_ID` = sum(is.na(`Customer ID`)))
```
merging the two information into one table, only for groups with NAs.
```{r}
df %>%
count(`NAs_in_Customer_ID` = is.na(`Customer ID`), Country, name = "#_of_rows") %>%
arrange(Country) %>%
group_by(Country) %>%
filter(n() > 1)
```
# - *column-wise removal*
If we wish to remove the columns with NAs we can select the ones where all their values are not NAs,
```{r}
df %>%
select(where(~ all(!is.na(.x))))
```
or deselect the ones where any of their values are NAs; these two commands are equivalent.
```{r}
df %>%
select(where(~ !any(is.na(.x))))
```
Of course we can also decide to select the columns with NAs, with either one of the following lines of code.
```{r}
df %>%
select(where(~ any(is.na(.x))))
df %>%
select(where(~ !all(!is.na(.x))))
```
A similar call can be useful to investigate if there are columns with only NAs.
```{r}
df %>%
select(where(~ all(is.na(.x))))
```
# - *row-wise removal*
If we wish to preserve the rows without NAs in a particular column we can write
```{r}
df %>%
filter(!is.na(Description))
```
Using the correct Boolean operator, we can also decide to preserve the rows without NAs in either one of the specified columns,
```{r}
df %>%
filter(!is.na(Description) |
!is.na(`Customer ID`))
```
or in all of them.
```{r}
df %>%
filter(!is.na(Description) &
!is.na(`Customer ID`))
```
If we yet don't know which columns have NAs, we can preserve the rows without them like this:
```{r}
df %>%
filter(if_all(everything(), ~ !is.na(.x)))
```
Using `if_any()` will instead be equivalent to preserving the rows without NAs in any given column, so only rows with all NAs will be removed in this case.
```{r}
df %>%
filter(if_any(everything(), ~ !is.na(.x)))
```
Of course we can also decide to preserve rows with NAs; we can do so by removing the negations from the previous five examples. With the first one we will only preserve the rows with NAs in the `Description` column.
```{r}
df %>%
filter(is.na(Description))
```
With the second one we will preserve rows with NAs in either one of the columns specified,
```{r}
df %>%
filter(is.na(Description) |
is.na(`Customer ID`))
```
which is here equivalent to the last one without negations, as all the rows with NAs in `Description` have NAs in `Customer ID`.
```{r}
df %>%
filter(if_any(everything(), ~ is.na(.x)))
```
With the third one the rows with NAs in all of the columns specified, again equivalent to a previous call given the NAs distribution of this data frame.
```{r}
df %>%
filter(is.na(Description) &
is.na(`Customer ID`))
```
And with the fourth example the rows that only have NAs (so none with this data frame).
```{r}
df %>%
filter(if_all(everything(), ~ is.na(.x)))
```
# - *substitution*
Another approach is to substitute NAs with another value; the following will work as long as the columns that contain them are of the same type of the value we want to input.
```{r}
df %>%
mutate(across(where(~ any(is.na(.x)) & is.character(.x)), ~ coalesce(.x, "missing")))
df %>%
mutate(across(where(~ any(is.na(.x)) & is.numeric(.x)), ~ coalesce(.x, 000000)))
```
The previous two calls can be combined into a single one.
```{r}
df %>%
mutate(across(where(~ any(is.na(.x)) & is.character(.x)), ~ coalesce(.x, "missing")),
across(where(~ any(is.na(.x)) & is.numeric(.x)), ~ coalesce(.x, 000000)))
```
If instead we want to recode a specific value to NA, we can proceed like this.
```{r}
df %>%
mutate(across(where(is.character), ~ na_if(., "Unspecified")),
across(where(is.numeric), ~ na_if(., 12)))
```
# - *other values besides NA*
NA is a special coded value, useful to identify missing values in data frames, but we can apply the previous procedures also to a value of choice, like for example "CHRISTMAS".
```{r}
df %>%
summarise(across(everything(), ~ any(stringr::str_detect(.x, "CHRISTMAS"), na.rm = TRUE)))
df %>%
summarise(across(everything(), ~ sum(stringr::str_detect(.x, "CHRISTMAS"), na.rm = TRUE)))
df %>%
summarise(across(everything(), ~ formattable::percent(mean(stringr::str_detect(.x, "CHRISTMAS"), na.rm = TRUE))))
```
We had to use a function from the `stringr` package to act on behalf of `is.na()`, and also to add `na.rm = TRUE` to not have NAs unfavourably modify the output (for `any()`/`all()` the reasons are explained [here](https://pykaalexandro.github.io/My-Dplyr-Handbook/any_all_variants.html#-_interactions_between_NAs__TRUE_or_FALSE) in the any(), all() & their variants page, but briefly `any(FALSE, NA)` returns `NA`).
```{r}
df %>%
summarise(across(everything(), ~ any(stringr::str_detect(.x, "CHRISTMAS"))))
```
To substitute, we can use `str_replace_all()`, always from the `stringr` package.
```{r}
df %>%
mutate(across(where(is.character), ~ stringr::str_replace_all(.x, "CHRISTMAS", "EASTER")))
```