Skip to contents

Detect or remove missing and infinite values in rvecs. Operations are done independently on each draw, though na.omit(), na.exclude(), and na.fail() also look across draws.

Usage

# S3 method for class 'rvec'
anyNA(x, recursive = FALSE)

# S3 method for class 'rvec'
is.na(x)

# S3 method for class 'rvec'
na.exclude(object, ...)

# S3 method for class 'rvec'
na.omit(object, ...)

Arguments

x, object

An rvec.

recursive

Whether anyNA() should be applied recursively to lists. Ignored when x is an rvec.

...

Currently ignored.

Value

Details

The behavior of the rvec methods for is.na(), is.nan(), is.finite(), and is.infinite() differs from the standard vctrs behavior, which is to return a logical vector with length equal to length(x). With rvecs, the standard vctrs behavior would entail summarising across draws, which is the job of the draws_* functions.

See also

Examples

x <- rvec(list(c(1.2, NA),
               c(Inf, 3),
               c(-1, NaN)))

## return a logical rvec
is.na(x)
#> <rvec_lgl<2>[3]>
#> [1] F,T F,F F,T
is.nan(x)
#> <rvec_lgl<2>[3]>
#> [1] F,F F,F F,T
is.finite(x)
#> <rvec_lgl<2>[3]>
#> [1] T,F F,T T,F
is.infinite(x)
#> <rvec_lgl<2>[3]>
#> [1] F,F T,F F,F

## return a logical rvec with length 1
anyNA(x)
#> <rvec_lgl<2>[1]>
#> [1] F,T

## summarise across draws
draws_any(anyNA(x))
#> [1] TRUE

## return an NA-free version of 'x'
na.omit(x)
#> <rvec_dbl<2>[1]>
#> [1] Inf,3
na.exclude(x)
#> <rvec_dbl<2>[1]>
#> [1] Inf,3

## use 'if_else_rvec' to modify values
## within rvec
if_else_rvec(is.na(x), 999, x)
#> <rvec_dbl<2>[3]>
#> [1] 1.2,999 Inf,3   -1,999 

## vctrs functions
library(vctrs, warn.conflicts = FALSE)
## all draws missing
vec_detect_missing(x)
#> [1] FALSE FALSE FALSE
## any draws missing
vec_detect_complete(x)
#> [1] FALSE  TRUE FALSE