Remove a row if all values on selected columns, or by default, on all columns, are missing, i.e. have values of NA or NaN.

drop_if_all_na(data, ...)

Arguments

data

A data frame

...

<tidy-select> Columns to inspect for missing values. If empty, all columns are used.

Value

A data frame, possibly with some rows dropped.

Details

The drop_na function will remove any row if it has any NA in selected columns. By default, it will remove the row there is any NA or NaN in any column. This drop_if_all_na function is similar but removes the row only if all values in the selected columns are NA or NaN. As with drop_na, by default it will use all columns. In other words, by default, drop_if_all_na removes any row if all values on that row are NA or NaN.

Examples

data_df <- data.frame(x = c(1, 2, NA, NA), y = c(2, NA, 5, NA))

drop_if_all_na(data_df)
#>    x  y
#> 1  1  2
#> 2  2 NA
#> 3 NA  5
drop_if_all_na(data_df, x)
#>   x  y
#> 1 1  2
#> 2 2 NA
drop_if_all_na(data_df, y)
#>    x y
#> 1  1 2
#> 2 NA 5
drop_if_all_na(data_df, x, y)
#>    x  y
#> 1  1  2
#> 2  2 NA
#> 3 NA  5
drop_if_all_na(data_df, x:y)
#>    x  y
#> 1  1  2
#> 2  2 NA
#> 3 NA  5
drop_if_all_na(data_df, starts_with('x'), ends_with('y'))
#>    x  y
#> 1  1  2
#> 2  2 NA
#> 3 NA  5