More complex wrapper around dplyr::filter(!is.na()) to remove NA rows using tidyselect. If any specified column contains an NA the whole row is removed. Reports the amount of rows removed containing NaN, NA, Inf, in that order. For example if one row contains Inf in one column and in another, the removed row will be counted in the NA tally.

filter_missing(.data, ..., remove_inf = TRUE)

# S3 method for data.frame
filter_missing(.data, ..., remove_inf = TRUE, condition = c("any", "all"))

Arguments

.data

dataframe

...

tidyselect. default selection is all columns

remove_inf

logical. default is to also remove Inf values. set to FALSE otherwise.

condition

defaults to "any". in which case removes rows if NA is in any specified column. "all" will remove rows only if each specified column is missing

Value

data frame

Details

S3 method, can also be used on vectors

Examples


tibble::tibble(x = c(NA, 1L, 2L, NA, NaN, 5L, Inf),
y = c(1L, NA, 2L, NA, Inf, 5L, Inf)) -> tbl1

tbl1
#> # A tibble: 7 × 2
#>       x     y
#>   <dbl> <dbl>
#> 1    NA     1
#> 2     1    NA
#> 3     2     2
#> 4    NA    NA
#> 5   NaN   Inf
#> 6     5     5
#> 7   Inf   Inf

# remove any row with a missing or Inf
tbl1 %>%
filter_missing()
#> Warning: Removed 1 rows containing NaN values
#> Warning: Removed 3 rows containing NA values
#> Warning: Removed 1 rows containing Inf values
#> # A tibble: 2 × 2
#>       x     y
#>   <dbl> <dbl>
#> 1     2     2
#> 2     5     5

# remove any row with Na or NaN in the x column
tbl1 %>%
filter_missing(x, remove_inf = FALSE)
#> Warning: Removed 1 rows containing NaN values
#> Warning: Removed 2 rows containing NA values
#> # A tibble: 4 × 2
#>       x     y
#>   <dbl> <dbl>
#> 1     1    NA
#> 2     2     2
#> 3     5     5
#> 4   Inf   Inf

# only remove rows where every entry is Na, NaN, or Inf
tbl1 %>%
filter_missing(condition = "all")
#> Warning: Removed 1 rows containing NA values
#> Warning: Removed 2 rows containing Inf values
#> # A tibble: 4 × 2
#>       x     y
#>   <dbl> <dbl>
#> 1    NA     1
#> 2     1    NA
#> 3     2     2
#> 4     5     5