Tabulates the values in a matrix by row (column).

rowTabulates(x, rows = NULL, cols = NULL, values = NULL, ...,
  useNames = TRUE)

colTabulates(x, rows = NULL, cols = NULL, values = NULL, ...,
  useNames = TRUE)

Arguments

x

An integer, a logical, or a raw NxK matrix.

rows

A vector indicating subset of rows to operate over. If NULL, no subsetting is done.

cols

A vector indicating subset of columns to operate over. If NULL, no subsetting is done.

values

An vector of J values of count. If NULL, all (unique) values are counted.

...

Not used.

useNames

If TRUE (default), names attributes of the result are set, otherwise not.

Value

Returns a NxJ (KxJ) matrix where N (K) is the number of row (column) vectors tabulated and J is the number of values counted.

Details

An alternative to these functions, is to use table(x, row(x)) and table(x, col(x)), with the exception that the latter do not support the raw data type. When there are no missing values in x, we have that all(rowTabulates(x) == t(table(x, row(x)))) and all(colTabulates(x) == t(table(x, col(x)))). When there are missing values, we have that all(rowTabulates(x) == t(table(x, row(x), useNA = "always")[, seq_len(nrow(x))])) and all(colTabulates(x) == t(table(x, col(x), useNA = "always")[, seq_len(ncol(x))])).

Author

Henrik Bengtsson

Examples

x <- matrix(1:5, nrow = 10, ncol = 5)
print(x)
#>       [,1] [,2] [,3] [,4] [,5]
#>  [1,]    1    1    1    1    1
#>  [2,]    2    2    2    2    2
#>  [3,]    3    3    3    3    3
#>  [4,]    4    4    4    4    4
#>  [5,]    5    5    5    5    5
#>  [6,]    1    1    1    1    1
#>  [7,]    2    2    2    2    2
#>  [8,]    3    3    3    3    3
#>  [9,]    4    4    4    4    4
#> [10,]    5    5    5    5    5
print(rowTabulates(x))
#>       1 2 3 4 5
#>  [1,] 5 0 0 0 0
#>  [2,] 0 5 0 0 0
#>  [3,] 0 0 5 0 0
#>  [4,] 0 0 0 5 0
#>  [5,] 0 0 0 0 5
#>  [6,] 5 0 0 0 0
#>  [7,] 0 5 0 0 0
#>  [8,] 0 0 5 0 0
#>  [9,] 0 0 0 5 0
#> [10,] 0 0 0 0 5
print(colTabulates(x))
#>      1 2 3 4 5
#> [1,] 2 2 2 2 2
#> [2,] 2 2 2 2 2
#> [3,] 2 2 2 2 2
#> [4,] 2 2 2 2 2
#> [5,] 2 2 2 2 2
# Count only certain values
print(rowTabulates(x, values = 1:3))
#>       1 2 3
#>  [1,] 5 0 0
#>  [2,] 0 5 0
#>  [3,] 0 0 5
#>  [4,] 0 0 0
#>  [5,] 0 0 0
#>  [6,] 5 0 0
#>  [7,] 0 5 0
#>  [8,] 0 0 5
#>  [9,] 0 0 0
#> [10,] 0 0 0


y <- as.raw(x)
dim(y) <- dim(x)
print(y)
#>       [,1] [,2] [,3] [,4] [,5]
#>  [1,]   01   01   01   01   01
#>  [2,]   02   02   02   02   02
#>  [3,]   03   03   03   03   03
#>  [4,]   04   04   04   04   04
#>  [5,]   05   05   05   05   05
#>  [6,]   01   01   01   01   01
#>  [7,]   02   02   02   02   02
#>  [8,]   03   03   03   03   03
#>  [9,]   04   04   04   04   04
#> [10,]   05   05   05   05   05
print(rowTabulates(y))
#>       0x1 0x2 0x3 0x4 0x5
#>  [1,]   5   0   0   0   0
#>  [2,]   0   5   0   0   0
#>  [3,]   0   0   5   0   0
#>  [4,]   0   0   0   5   0
#>  [5,]   0   0   0   0   5
#>  [6,]   5   0   0   0   0
#>  [7,]   0   5   0   0   0
#>  [8,]   0   0   5   0   0
#>  [9,]   0   0   0   5   0
#> [10,]   0   0   0   0   5
print(colTabulates(y))
#>      0x1 0x2 0x3 0x4 0x5
#> [1,]   2   2   2   2   2
#> [2,]   2   2   2   2   2
#> [3,]   2   2   2   2   2
#> [4,]   2   2   2   2   2
#> [5,]   2   2   2   2   2