/usr/share/doc/r-cran-matrixstats/tests/binCounts.R is in r-cran-matrixstats 0.14.2-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 | library("matrixStats")
library("stats")
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Local functions
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
binCounts_hist <- function(x, bx, right=FALSE, ...) {
n0 <- graphics::hist(x, breaks=bx, right=right, include.lowest=TRUE, plot=FALSE)$counts
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Non-sorted and sorted positions
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
nx <- 1e5 # Number of data points
nb <- 2e3 # Number of bins
# Uniformely distributed bins
bx <- seq(from=0, to=1, length.out=nb+1L)
bx <- c(-1, bx, 2)
# Sample data points
set.seed(0xBEEF)
x <- runif(nx, min=0, max=1)
for (kk in 1:2) {
n0 <- binCounts_hist(x, bx=bx)
n1 <- binCounts(x, bx=bx)
# Sanity check
stopifnot(identical(n1, n0))
# Check reversed
n1r <- rev(binCounts(-x, bx=rev(-bx), right=TRUE))
stopifnot(identical(n1r, n1))
# Retry with a sorted vector
x <- sort(x)
} # for (kk in 1:2)
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Missing values
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
x <- 1:200
x[100] <- NA_integer_
nx <- length(x)
# Bins
bx <- c(0.5,50.5,100.5,150.5,200.5)
yS0 <- binCounts_hist(x, bx=bx)
yS <- binCounts(x, bx=bx)
# Sanity check
stopifnot(all.equal(yS, yS0))
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Border cases
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
x <- 1:10
bx <- min(x) - c(10,1)
yS <- binCounts(x, bx=bx)
stopifnot(all.equal(yS, 0L))
bx <- range(x)
yS <- binCounts(x, bx=bx)
stopifnot(all.equal(yS, length(x)-1L))
bx <- max(x) + c(1,10)
yS <- binCounts(x, bx=bx)
stopifnot(all.equal(yS, 0L))
# Every second empty
x <- 1:10
bx <- rep(x, each=2L)
yS <- binCounts(x, bx=bx)
stopifnot(all.equal(yS, rep(c(0L,1L), length.out=length(bx)-1L)))
## NOTE: binCounts_hist() does not give the same last bin count
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Exception handling
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Zero bin bounderies (invalid bin definition)
bx <- double(0L)
res <- try(yS <- binCounts(1:10, bx=bx), silent=TRUE)
stopifnot(inherits(res, "try-error"))
# One bin boundery (invalid bin definition)
bx <- double(1L)
res <- try(yS <- binCounts(1:10, bx=bx), silent=TRUE)
stopifnot(inherits(res, "try-error"))
|