[R] apply ( , , table)

White.Denis@epamail.epa.gov White.Denis at epamail.epa.gov
Tue Aug 24 18:42:58 CEST 2004





a <- matrix (c(
    7, 1, 1, 2, 6,
    3, 4, 0, 1, 4,
    5, 1, 8, 4, 4,
    6, 1, 1, 2, 5), nrow=4, byrow=TRUE)

b <- apply (a, 1, table)

"apply" documentation says clearly that if the rows of the result of FUN
are the same length, then an array will be returned.  And column-major
would be the appropriate order in R.  But "b" above is pretty opaque
compared to what one would expect, and what one would get from "apply (
, , table)" if the rows were not of equal length.  One needs to do
something like

n <- matrix (apply (a, 1, function (x) unique (sort (x))), nrow=nrow(a))

to get the corresponding "names" of "b" to figure out the counts.

Denis White




More information about the R-help mailing list