Skip to contents

Remove missing values from an atomic vector using a pipe-compatible function.

Usage

rm_na(x)

rmna(x)

Arguments

x

(vector)
The vector to remove missing values from.

Value

A vector of the same type and class as x with NA values removed.

Details

  • NaN values are also removed because is.na(NaN) returns TRUE.

  • NULL input returns NULL because anyNA(NULL) is FALSE.

  • Zero-length input is returned unchanged.

  • Attributes such as names, factor levels, and Date/POSIXct class are preserved by the [ subset.

Examples

#----------------------------------------------------------------------------
# rm_na() examples
#----------------------------------------------------------------------------
library(bkbase)

# No NAs: input is returned unchanged
rm_na(1:3)
#> [1] 1 2 3

# NAs are removed
rm_na(c(1, NA, 2, NA, 3))
#> [1] 1 2 3

# NaN is also removed
rm_na(c(1, NaN, 2))
#> [1] 1 2

# Names and other attributes are preserved
rm_na(c(a = 1, b = NA, c = 3))
#> a c 
#> 1 3 
rm_na(factor(c("a", NA, "b")))
#> [1] a b
#> Levels: a b

# rmna() is an alias for rm_na()
c(1, NA, 2) |> rmna()
#> [1] 1 2