This function converts an xtabs object to a matrix.

# S3 method for xtabs
as.matrix(x, ...)

Arguments

x

an object of class xtabs

...

additional arguments to be passed to or from methods.

Details

An xtabs object is indeed already a matrix, but won't be converted to a pure matrix by as.matrix.default function, as its class definition will remain unchanged. Some functions expecting a pure matrix may fail, when fed with a xtabs object. as.matrix.xtabs will drop the classes and the call attribute.
Note that unclass would as well discard the classes xtabs and table, but retain the "call" attribute.

Author

Andri Signorell <andri@signorell.net>

See also

Examples

tab <- xtabs( ~ driver + operator, data=d.pizza)

str(tab)
#>  'xtabs' int [1:7, 1:3] 30 58 55 28 68 25 99 8 87 117 ...
#>  - attr(*, "dimnames")=List of 2
#>   ..$ driver  : chr [1:7] "Butcher" "Carpenter" "Carter" "Farmer" ...
#>   ..$ operator: chr [1:3] "Allanah" "Maria" "Rhonda"
#>  - attr(*, "call")= language xtabs(formula = ~driver + operator, data = d.pizza)
class(tab)
#> [1] "xtabs" "table"

str(as.matrix(tab))
#>  int [1:7, 1:3] 30 58 55 28 68 25 99 8 87 117 ...
#>  - attr(*, "dimnames")=List of 2
#>   ..$ driver  : chr [1:7] "Butcher" "Carpenter" "Carter" "Farmer" ...
#>   ..$ operator: chr [1:3] "Allanah" "Maria" "Rhonda"
class(as.matrix(tab))
#> [1] "matrix" "array"