Methods for generic functions anyNA(), is.na(), is.nan(), is.infinite(), and is.finite(), for objects inheriting from virtual class Matrix or sparseVector.

# S4 method for class 'denseMatrix'
is.na(x)
# S4 method for class 'sparseMatrix'
is.na(x)
# S4 method for class 'diagonalMatrix'
is.na(x)
# S4 method for class 'indMatrix'
is.na(x)
# S4 method for class 'sparseVector'
is.na(x)
## ...
## and likewise for anyNA, is.nan, is.infinite, is.finite

Arguments

x

an R object, here a sparse or dense matrix or vector.

Value

For is.*(), an nMatrix or nsparseVector matching the dimensions of x and specifying the positions in x of (some subset of) NA, NaN, Inf, and -Inf. For anyNA(), TRUE if x contains NA or NaN and FALSE otherwise.

See also

Examples

(M <- Matrix(1:6, nrow = 4, ncol = 3,
             dimnames = list(letters[1:4], LETTERS[1:3])))
#> 4 x 3 Matrix of class "dgeMatrix"
#>   A B C
#> a 1 5 3
#> b 2 6 4
#> c 3 1 5
#> d 4 2 6
stopifnot(!anyNA(M), !any(is.na(M)))

M[2:3, 2] <- NA
(inM <- is.na(M))
#> 4 x 3 Matrix of class "ngeMatrix"
#>       A     B     C
#> a FALSE FALSE FALSE
#> b FALSE  TRUE FALSE
#> c FALSE  TRUE FALSE
#> d FALSE FALSE FALSE
stopifnot(anyNA(M), sum(inM) == 2)

(A <- spMatrix(nrow = 10, ncol = 20,
               i = c(1, 3:8), j = c(2, 9, 6:10), x = 7 * (1:7)))
#> 10 x 20 sparse Matrix of class "dgTMatrix"
#>                                                   
#>  [1,] . 7 . . .  .  .  .  .  . . . . . . . . . . .
#>  [2,] . . . . .  .  .  .  .  . . . . . . . . . . .
#>  [3,] . . . . .  .  .  . 14  . . . . . . . . . . .
#>  [4,] . . . . . 21  .  .  .  . . . . . . . . . . .
#>  [5,] . . . . .  . 28  .  .  . . . . . . . . . . .
#>  [6,] . . . . .  .  . 35  .  . . . . . . . . . . .
#>  [7,] . . . . .  .  .  . 42  . . . . . . . . . . .
#>  [8,] . . . . .  .  .  .  . 49 . . . . . . . . . .
#>  [9,] . . . . .  .  .  .  .  . . . . . . . . . . .
#> [10,] . . . . .  .  .  .  .  . . . . . . . . . . .
stopifnot(!anyNA(A), !any(is.na(A)))

A[2, 3] <- A[1, 2] <- A[5, 5:9] <- NA
(inA <- is.na(A))
#> 10 x 20 sparse Matrix of class "ngTMatrix"
#>                                              
#>  [1,] . | . . . . . . . . . . . . . . . . . .
#>  [2,] . . | . . . . . . . . . . . . . . . . .
#>  [3,] . . . . . . . . . . . . . . . . . . . .
#>  [4,] . . . . . . . . . . . . . . . . . . . .
#>  [5,] . . . . | | | | | . . . . . . . . . . .
#>  [6,] . . . . . . . . . . . . . . . . . . . .
#>  [7,] . . . . . . . . . . . . . . . . . . . .
#>  [8,] . . . . . . . . . . . . . . . . . . . .
#>  [9,] . . . . . . . . . . . . . . . . . . . .
#> [10,] . . . . . . . . . . . . . . . . . . . .
stopifnot(anyNA(A), sum(inA) == 1 + 1 + 5)