Modifies tabulate to work with non-positive integers too.

TabulateInt(vec)

Arguments

vec

The input vector

Value

A named integer vector. There is a bin for each of the values 1, ..., nbins.

Note

The behavior on non-integers might be somewhat unpredictable, but should be somewhat like using table(cut(...)) with breaks being from the minimum to the maximum + 1. See the "Examples" section.

Author

Ananda Mahto

Examples

x <- c(-5, -5, 3, 1, 0, 2, 5, -4, 0, 0, 1) TabulateInt(x)
#> -5 -4 -3 -2 -1 0 1 2 3 4 5 #> 2 1 0 0 0 3 2 1 1 0 1
## Compare tabulate(x)
#> [1] 2 1 1 0 1
#> x #> -5 -4 0 1 2 3 5 #> 2 1 3 2 1 1 1
table(factor(x, min(x):max(x)))
#> #> -5 -4 -3 -2 -1 0 1 2 3 4 5 #> 2 1 0 0 0 3 2 1 1 0 1
## Non-integers set.seed(1) x <- rnorm(20) TabulateInt(x)
#> -2.2146998871775 -1.2146998871775 -0.2146998871775 0.7853001128225 #> 1 5 9 5
table(cut(x, seq(min(x), max(x)+1, 1), include.lowest = TRUE))
#> #> [-2.21,-1.21] (-1.21,-0.215] (-0.215,0.785] (0.785,1.79] #> 1 5 9 5