-
Notifications
You must be signed in to change notification settings - Fork 4
/
README.rmd
628 lines (313 loc) · 14.4 KB
/
README.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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
---
date: "`r Sys.Date()`"
output: github_document
title: "mde: Missing Data Explorer"
vignette: >
%\VignetteIndexEntry{mde-Missing-Data-Explorer}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
resource_files:
- man/figures/mde_icon_2.png
---
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>"
)
```
<!-- badges: start -->
[![DOI](https://zenodo.org/badge/DOI/10.5281/zenodo.3890659.svg)](https://doi.org/10.5281/zenodo.3890659)
[![CRAN_Status_Badge](https://r-pkg.org/badges/version/mde)](https://cran.r-project.org/package=mde)
[![CRAN\_Release\_Badge](https://www.r-pkg.org/badges/version-ago/mde)](https://CRAN.R-project.org/package=mde)
[![Codecov test coverage](https://codecov.io/gh/Nelson-Gon/mde/branch/master/graph/badge.svg)](https://codecov.io/gh/Nelson-Gon/mde?branch=master)
[![R-CMD-check](https://github.com/Nelson-Gon/mde/actions/workflows/devel-check.yaml/badge.svg)](https://github.com/Nelson-Gon/mde/actions/workflows/devel-check.yaml)
![test-coverage](https://github.com/Nelson-Gon/mde/workflows/test-coverage/badge.svg)
[![Project Status](https://www.repostatus.org/badges/latest/active.svg)](https://www.repostatus.org/)
[![lifecycle](https://img.shields.io/badge/lifecycle-maturing-blue.svg)](https://lifecycle.r-lib.org/articles/stages.html)
[![license](https://img.shields.io/badge/license-GPL--3-blue.svg)](https://www.gnu.org/licenses/gpl-3.0.en.html)
[![Downloads](https://cranlogs.r-pkg.org/badges/mde)](https://cran.r-project.org/package=mde)
[![TotalDownloads](https://cranlogs.r-pkg.org/badges/grand-total/mde?color=green)](https://cran.r-project.org/package=mde)
[![GitHub last commit](https://img.shields.io/github/last-commit/Nelson-Gon/mde.svg)](https://github.com/Nelson-Gon/mde/commits/master)
[![GitHub issues](https://img.shields.io/github/issues/Nelson-Gon/mde.svg)](https://github.com/Nelson-Gon/mde/issues/)
[![GitHub issues-closed](https://img.shields.io/github/issues-closed/Nelson-Gon/mde.svg)](https://github.com/Nelson-Gon/mde/issues?q=is%3Aissue+is%3Aclosed)
[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square)](https://makeapullrequest.com)
[![Maintenance](https://img.shields.io/badge/Maintained%3F-yes-green.svg)](https://github.com/Nelson-Gon/mde/graphs/commit-activity)
<!-- badges: end -->
<img src='https://github.com/Nelson-Gon/mde/blob/master/man/figures/mde_icon_2.png?raw=true' align="right" height="120" width="120"/>
The goal of `mde` is to ease exploration of missingness.
**Installation**
**CRAN release**
```{r, eval=FALSE}
install.packages("mde")
```
**Stable Development version**
```{r, eval = FALSE}
devtools::install_github("Nelson-Gon/mde")
devtools::install_github("Nelson-Gon/mde", build_vignettes=TRUE)
```
**Unstable Development version**
```{r, eval=FALSE}
devtools::install_github("Nelson-Gon/mde@develop")
```
**Loading the package**
```{r pkg_load}
library(mde)
```
## Exploring missingness
To get a simple missingness report, use `na_summary`:
```{r na_summary_default}
na_summary(airquality)
```
To sort this summary by a given column :
```{r column_sort_na_summary}
na_summary(airquality,sort_by = "percent_complete")
```
If one would like to reset (drop) row names, then one can set `row_names` to
`TRUE` This may especially be useful in cases where `rownames` are simply
numeric and do not have much additional use.
```{r reset_rownames}
na_summary(airquality,sort_by = "percent_complete", reset_rownames = TRUE)
```
To sort by `percent_missing` instead:
```{r}
na_summary(airquality, sort_by = "percent_missing")
```
To sort the above in descending order:
```{r}
na_summary(airquality, sort_by="percent_missing", descending = TRUE)
```
To exclude certain columns from the analysis:
```{r}
na_summary(airquality, exclude_cols = c("Day", "Wind"))
```
To include or exclude via regex match:
```{r}
na_summary(airquality, regex_kind = "inclusion",pattern_type = "starts_with", pattern = "O|S")
```
```{r}
na_summary(airquality, regex_kind = "exclusion",pattern_type = "regex", pattern = "^[O|S]")
```
To get this summary by group:
```{r}
test2 <- data.frame(ID= c("A","A","B","A","B"), Vals = c(rep(NA,4),"No"),ID2 = c("E","E","D","E","D"))
na_summary(test2,grouping_cols = c("ID","ID2"))
```
```{r}
na_summary(test2, grouping_cols="ID")
```
* `get_na_counts`
This provides a convenient way to show the number of missing values column-wise. It is relatively fast(tests done on about 400,000 rows, took a few microseconds.)
To get the number of missing values in each column of `airquality`, we can use the function as follows:
```{r}
get_na_counts(airquality)
```
The above might be less useful if one would like to get the results by group. In that case, one can provide a grouping vector of names in `grouping_cols`.
```{r}
test <- structure(list(Subject = structure(c(1L, 1L, 2L, 2L), .Label = c("A",
"B"), class = "factor"), res = c(NA, 1, 2, 3), ID = structure(c(1L,
1L, 2L, 2L), .Label = c("1", "2"), class = "factor")), class = "data.frame", row.names = c(NA,
-4L))
get_na_counts(test, grouping_cols = "ID")
```
* `percent_missing`
This is a very simple to use but quick way to take a look at the percentage of data that is missing column-wise.
```{r}
percent_missing(airquality)
```
We can get the results by group by providing an optional `grouping_cols` character vector.
```{r}
percent_missing(test, grouping_cols = "Subject")
```
To exclude some columns from the above exploration, one can provide an optional character vector in `exclude_cols`.
```{r}
percent_missing(airquality,exclude_cols = c("Day","Temp"))
```
* `sort_by_missingness`
This provides a very simple but relatively fast way to sort variables by missingness. Unless otherwise stated, this does not currently support arranging grouped percents.
Usage:
```{r}
sort_by_missingness(airquality, sort_by = "counts")
```
To sort in descending order:
```{r}
sort_by_missingness(airquality, sort_by = "counts", descend = TRUE)
```
To use percentages instead:
```{r}
sort_by_missingness(airquality, sort_by = "percents")
```
## Recoding as NA
* `recode_as_na`
As the name might imply, this converts any value or vector of values to `NA` i.e. we take a value such as "missing" or "NA" (not a real `NA` according to `R`) and convert it to R's known handler for missing values (`NA`).
To use the function out of the box (with default arguments), one simply does something like:
```{r}
dummy_test <- data.frame(ID = c("A","B","B","A"),
values = c("n/a",NA,"Yes","No"))
# Convert n/a and no to NA
head(recode_as_na(dummy_test, value = c("n/a","No")))
```
Great, but I want to do so for specific columns not the entire dataset. You can do this by providing column names to `subset_cols`.
```{r}
another_dummy <- data.frame(ID = 1:5, Subject = 7:11,
Change = c("missing","n/a",2:4 ))
# Only change values at the column Change
head(recode_as_na(another_dummy, subset_cols = "Change", value = c("n/a","missing")))
```
To recode columns using [RegEx](https://en.wikipedia.org/wiki/Regular_expression),one can provide `pattern_type` and a target `pattern`. Currently supported `pattern_types` are `starts_with`, `ends_with`, `contains` and `regex`. See docs for more details.:
```{r}
# only change at columns that start with Solar
head(recode_as_na(airquality,value=190,pattern_type="starts_with",pattern="Solar"))
```
```{r}
# recode at columns that start with O or S(case sensitive)
head(recode_as_na(airquality,value=c(67,118),pattern_type="starts_with",pattern="S|O"))
```
```{r}
# use my own RegEx
head(recode_as_na(airquality,value=c(67,118),pattern_type="regex",pattern="(?i)^(s|o)"))
```
* `recode_as_na_if`
This function allows one to deliberately introduce missing values if a column meets a certain threshold of missing values. This is similar to `amputation` but is much more basic. It is only provided here because it is hoped it may be useful to someone for whatever reason.
```{r}
head(recode_as_na_if(airquality,sign="gt", percent_na=20))
```
* `recode_as_na_str`
This allows recoding as `NA` based on a string match.
```{r}
partial_match <- data.frame(A=c("Hi","match_me","nope"), B=c(NA, "not_me","nah"))
recode_as_na_str(partial_match,"ends_with","ME", case_sensitive=FALSE)
```
* `recode_as_na_for`
For all values greater/less/less or equal/greater or equal than some value, can I convert them to `NA`?!
**Yes You Can!** All we have to do is use `recode_as_na_for`:
```{r}
head(recode_as_na_for(airquality,criteria="gt",value=25))
```
To do so at specific columns, pass an optional `subset_cols` character vector:
```{r}
head(recode_as_na_for(airquality, value=40,subset_cols=c("Solar.R","Ozone"), criteria="gt"))
```
## Recoding NA as
* `recode_na_as`
Sometimes, for whatever reason, one would like to replace `NA`s with whatever value they would like. `recode_na_as` provides a very simple way to do just that.
```{r}
head(recode_na_as(airquality))
# use NaN
head(recode_na_as(airquality, value=NaN))
```
As a "bonus", you can manipulate the data only at specific columns as shown here:
```{r}
head(recode_na_as(airquality, value=0, subset_cols="Ozone"))
```
The above also supports custom recoding similar to `recode_na_as`:
```{r}
head(mde::recode_na_as(airquality, value=0, pattern_type="starts_with",pattern="Solar"))
```
* `column_based_recode`
Ever needed to change values in a given column based on the proportions of `NA`s in other columns(row-wise)?!. The goal of `column_based_recode` is to achieve just that. Let's see how we could do this with a simple example:
```{r}
head(column_based_recode(airquality, values_from = "Wind", values_to="Wind", pattern_type = "regex", pattern = "Solar|Ozone"))
```
* `custom_na_recode`
This allows recoding `NA` values with common stats functions such as `mean`,`max`,`min`,`sd`.
To use default values:
```{r}
head(custom_na_recode(airquality))
```
To use select columns:
```{r}
head(custom_na_recode(airquality,func="mean",across_columns=c("Solar.R","Ozone")))
```
To use a function from another package to perform replacements:
To perform a forward fill with `dplyr`'s `lead`:
```{r}
# use lag for a backfill
head(custom_na_recode(airquality,func=dplyr::lead ))
```
To perform replacement by group:
```{r}
some_data <- data.frame(ID=c("A1","A1","A1","A2","A2", "A2"),A=c(5,NA,0,8,3,4),B=c(10,0,0,NA,5,6),C=c(1,NA,NA,25,7,8))
head(custom_na_recode(some_data,func = "mean", grouping_cols = "ID"))
```
Across specific columns:
```{r}
head(custom_na_recode(some_data,func = "mean", grouping_cols = "ID", across_columns = c("C", "A")))
```
* `recode_na_if`
Given a `data.frame` object, one can recode `NA`s as another value based on a grouping variable. In the example below, we replace all `NA`s in all columns with 0s if the ID is `A2` or `A3`
```{r}
some_data <- data.frame(ID=c("A1","A2","A3", "A4"),
A=c(5,NA,0,8), B=c(10,0,0,1),
C=c(1,NA,NA,25))
head(recode_na_if(some_data,grouping_col="ID", target_groups=c("A2","A3"),
replacement= 0))
```
## Dropping NAs
* `drop_na_if`
Suppose you wanted to drop any column that has a percentage of `NA`s greater than or equal to a certain value? `drop_na_if` does just that.
We can drop any columns that have greater than or equal(gteq) to 24% of the values missing from `airquality`:
```{r}
head(drop_na_if(airquality, sign="gteq",percent_na = 24))
```
The above also supports less than or equal to(`lteq`), equal to(`eq`), greater than(`gt`) and less than(`lt`).
To keep certain columns despite fitting the target `percent_na` criteria, one can provide an optional `keep_columns` character vector.
```{r}
head(drop_na_if(airquality, percent_na = 24, keep_columns = "Ozone"))
```
Compare the above result to the following:
```{r}
head(drop_na_if(airquality, percent_na = 24))
```
To drop groups that meet a set missingness criterion, we proceed as follows.
```{r}
grouped_drop <- structure(list(ID = c("A", "A", "B", "A", "B"),
Vals = c(4, NA, NA, NA, NA), Values = c(5, 6, 7, 8, NA)),
row.names = c(NA, -5L), class = "data.frame")
# Drop all columns for groups that meet a percent missingness of greater than or
# equal to 67
drop_na_if(grouped_drop,percent_na = 67,sign="gteq",
grouping_cols = "ID")
```
* `drop_row_if`
This is similar to `drop_na_if` but does operations rowwise not columnwise. Compare to the example above:
```{r}
# Drop rows with at least two NAs
head(drop_row_if(airquality, sign="gteq", type="count" , value = 2))
```
To drop based on percentages:
```{r}
# Drops 42 rows
head(drop_row_if(airquality, type="percent", value=16, sign="gteq",
as_percent=TRUE))
```
For more details, please see the documentation of `drop_row_if`.
* `drop_na_at`
This provides a simple way to drop missing values only at specific columns. It currently only returns those columns with their missing values removed. See usage below. Further details are given in the documentation. It is currently case sensitive.
```{r}
head(drop_na_at(airquality,pattern_type = "starts_with","O"))
```
* `drop_all_na`
This drops columns where all values are missing.
```{r}
test2 <- data.frame(ID= c("A","A","B","A","B"), Vals = c(4,rep(NA, 4)))
drop_all_na(test2, grouping_cols="ID")
```
Alternatively, we can drop groups where all variables are all NA.
```{r}
test2 <- data.frame(ID= c("A","A","B","A","B"), Vals = rep(NA, 5))
head(drop_all_na(test, grouping_cols = "ID"))
```
* `dict_recode`
If one would like to recode column values using a "dictionary", `dict_recode` provides a simple way to do that. For example, if one would like to convert `NA` values in `Solar.R` to 520 and those in `Ozone` to 42, one simply calls the following:
```{r}
head(dict_recode(airquality, use_func="recode_na_as",
patterns = c("solar", "ozone"),
pattern_type="starts_with", values = c(520,42)))
```
---
Please note that the `mde` project is released with a
[Contributor Code of Conduct](https://github.com/Nelson-Gon/mde/blob/master/.github/CODE_OF_CONDUCT.md).
By contributing to this project, you agree to abide by its terms.
For further exploration, please `browseVignettes("mde")`.
To raise an issue, please do so [here](https://github.com/Nelson-Gon/mde/issues)
Thank you, feedback is always welcome :)