/
filter_missing.R
192 lines (144 loc) · 4.11 KB
/
filter_missing.R
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
#' remove nas
#'
#' @param x vec
#' @keywords internal
#'
#' @return vec
#'
remove_nas <- function(x){
x[which(!is.na(x))]
}
#' remove nans
#'
#' @param x vec
#' @keywords internal
#'
#' @return vec
#'
remove_nans <- function(x){
x[which(!is.nan(x))]
}
#' remove infs
#'
#' @param x vec
#' @keywords internal
#'
#' @return vec
#'
remove_infs <- function(x){
x[which(!is.infinite(x))]
}
#' is missing or inf
#'
#' @param x
#'
#' @return logical
#' @keywords internal
#'
is_missing_or_inf <- function(x){
is.na(x) | is.infinite(x)
}
#' @rdname filter_missing.data.frame
#' @param remove_inf logical. default is to also remove \code{Inf} values. set to \code{FALSE} otherwise.
#' @export
filter_missing <- function(.data, ..., remove_inf = TRUE){
UseMethod("filter_missing", .data)
}
#' filter out missings
#'
#' More complex wrapper around \code{dplyr::filter(!is.na())} to remove \code{NA} rows using tidyselect. If any specified column contains an \code{NA}
#' the whole row is removed. Reports the amount of rows removed containing \code{NaN}, \code{NA}, \code{Inf}, in that order.
#' For example if one row contains \code{Inf} in one column and in another, the removed row will be counted in the \code{NA} tally.
#'
#' S3 method, can also be used on vectors
#'
#' @method filter_missing data.frame
#' @param .data dataframe
#' @param ... tidyselect. default selection is all columns
#' @param remove_inf logical. default is to also remove \code{Inf} values. set to \code{FALSE} otherwise.
#' @param condition defaults to "any". in which case removes rows if \code{NA} is in any specified column. "all" will remove rows only if each specified column is missing
#'
#' @return data frame
#' @export
#'
#' @examples
#'
#' tibble::tibble(x = c(NA, 1L, 2L, NA, NaN, 5L, Inf),
#' y = c(1L, NA, 2L, NA, Inf, 5L, Inf)) -> tbl1
#'
#' tbl1
#'
#' # remove any row with a missing or Inf
#' tbl1 %>%
#' filter_missing()
#'
#' # remove any row with Na or NaN in the x column
#' tbl1 %>%
#' filter_missing(x, remove_inf = FALSE)
#'
#' # only remove rows where every entry is Na, NaN, or Inf
#' tbl1 %>%
#' filter_missing(condition = "all")
filter_missing.data.frame <- function(.data, ..., remove_inf = TRUE, condition = c("any", "all")){
condition <- match.arg(condition)
if(condition == "any"){
filter_condition <- dplyr::if_any
} else {
filter_condition <- dplyr::if_all
}
.data %>%
select_otherwise(..., otherwise = tidyselect::everything()) -> cols
nrow(.data) -> rows1
.data %>%
dplyr::filter(!filter_condition(tidyselect::any_of(cols), is.nan)) -> .data
nrow(.data) -> rows2
rows1 - rows2 -> nan_diff
if(nan_diff > 0){
warning(stringr::str_c("Removed ",nan_diff, " rows containing NaN values"), call. = FALSE)
}
.data %>%
dplyr::filter(!filter_condition(tidyselect::any_of(cols), is.na)) -> .data
nrow(.data) -> rows3
rows2 - rows3 -> na_diff
if(na_diff > 0){
warning(stringr::str_c("Removed ",na_diff, " rows containing NA values"), call. = FALSE)
}
if(remove_inf){
.data %>%
dplyr::filter(!filter_condition(tidyselect::any_of(cols), is_missing_or_inf)) -> .data
nrow(.data) -> rows4
rows3 - rows4 -> inf_diff
if(inf_diff > 0){
warning(stringr::str_c("Removed ",inf_diff, " rows containing Inf values"), call. = FALSE)
}
}
.data
}
#' @export
filter_missing.default<- function(.data, ..., remove_inf = TRUE){
length(.data) -> elements1
.data %>%
remove_nans() -> .data
length(.data) -> elements2
elements1 - elements2 -> nan_diff
if(nan_diff > 0){
warning(stringr::str_c("Removed ",nan_diff, " elements containing NaN values"), call. = FALSE)
}
.data %>%
remove_nas()-> .data
length(.data) -> elements3
elements2 - elements3 -> na_diff
if(na_diff > 0){
warning(stringr::str_c("Removed ",na_diff, " elements containing NA values"), call. = FALSE)
}
if(remove_inf){
.data %>%
remove_infs() -> .data
length(.data) -> elements4
elements3 - elements4 -> inf_diff
if(inf_diff > 0){
warning(stringr::str_c("Removed ",inf_diff, " elements containing Inf values"), call. = FALSE)
}
}
.data
}