File: allocVector.R

package info (click to toggle)
r-cran-matrixstats 1.5.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,104 kB
  • sloc: ansic: 7,300; sh: 11; makefile: 2
file content (25 lines) | stat: -rw-r--r-- 633 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
library("matrixStats")

allocVector_R <- function(length, value = NA) {
  x <- vector(mode = typeof(value), length = length)
  if (!is.finite(value) || value != 0) x[] <- value
  x
}

values <- list(
  -1L, 0L, +1L, NA_integer_, .Machine$integer.max,
  -1, 0, +1, NA_real_, NaN, -Inf, +Inf,
  .Machine$double.xmin, .Machine$double.xmax,
  .Machine$double.eps, .Machine$double.neg.eps,
  FALSE, TRUE, NA
)

n <- 10L
for (value in values) {
  x0 <- allocVector_R(n, value = value)
  x <- allocVector(n, value = value)
  if (!identical(x, x0)) {
    str(list(n = n, value = value, x = x, x0 = x0))
    stopifnot(identical(x, x0))
  }
}