R: Tabulation for Vectors (original) (raw)

tabulate {base} R Documentation

Description

tabulate takes the integer-valued vector bin and counts the number of times each integer occurs in it.

Usage

tabulate(bin, nbins = max(1, bin, na.rm = TRUE))

Arguments

bin a numeric vector (of positive integers), or a factor.Long vectors are supported.
nbins the number of bins to be used.

Details

tabulate is the workhorse for the [table](../../base/help/table.html) function.

If bin is a factor, its internal integer representation is tabulated.

If the elements of bin are numeric but not integers, they are truncated by [as.integer](../../base/help/as.integer.html).

Value

An integer valued [integer](../../base/help/integer.html) or [double](../../base/help/double.html) vector (without names). There is a bin for each of the values 1, ..., nbins; values outside that range and NAs are (silently) ignored.

On 64-bit platforms bin can have 2^{31} or more elements (i.e., length(bin) > .Machine$integer.max), and hence a count could exceed the maximum integer. For this reason, the return value is of type double for such long bin vectors.

See Also

[table](../../base/help/table.html), [factor](../../base/help/factor.html).

Examples

tabulate(c(2,3,5))
tabulate(c(2,3,3,5), nbins = 10)
tabulate(c(-2,0,2,3,3,5))  # -2 and 0 are ignored
tabulate(c(-2,0,2,3,3,5), nbins = 3)
tabulate(factor(letters[1:10]))

[Package _base_ version 4.6.0 Index]