In one pass over the vector NAs are treated according to parameter na.last exactly like sort() does, the range(), number of NAs and unsortedness is determined.

range_sortna(x, decreasing = FALSE, na.last = NA)

Arguments

x

an integer vector

decreasing

(currently only FALSE is supported)

na.last

NA removes NAs, FALSE puts NAs at the beginning, TRUE puts NAs at the end

Value

an integer vector with NAs are treated and an attribute range_na() with four elements:

  1. min integer

  2. max integer

  3. number of NAs

  4. 0 for sorted vector and 1 for is.unsorted()

Examples

range_sortna(c(0L, 1L, NA, 2L))
#> [1] 0 1 2
#> attr(,"range_sortna")
#> [1] 0 2 0 0
range_sortna(c(2L, NA, 1L, 0L))
#> [1] 2 1 0
#> attr(,"range_sortna")
#> [1] 0 2 0 1
range_sortna(c(0L, 1L, NA, 2L), na.last=TRUE)
#> [1]  0  1  2 NA
#> attr(,"range_sortna")
#> [1] 0 2 1 0
range_sortna(c(2L, NA, 1L, 0L), na.last=TRUE)
#> [1]  2  1  0 NA
#> attr(,"range_sortna")
#> [1] 0 2 1 1
range_sortna(c(0L, 1L, NA, 2L), na.last=FALSE)
#> [1] NA  0  1  2
#> attr(,"range_sortna")
#> [1] 0 2 1 0
range_sortna(c(2L, NA, 1L, 0L), na.last=FALSE)
#> [1] NA  2  1  0
#> attr(,"range_sortna")
#> [1] 0 2 1 1