Working with Tables
To begin exploring R tables, consider this example:
> u <- c(22,8,33,6,8,29,-2) > fl <- list(c(5,12,13,12,13,5,13),c("a","bc","a","a","bc","a","a")) > tapply(u,fl,length) a bc 5 2 NA 12 1 1 13 2 1
Here, tapply()
again temporarily breaks u
into subvectors, as you saw earlier, and then applies the length()
function to each subvector. (Note that this is independent of what’s in u
. Our focus now is purely on the factors.) Those subvector lengths are the counts of the occurrences of each of the 3 × 2 = 6 combinations of the two factors. For instance, 5 occurred twice with "a"
and not at all with "bc";
hence the entries 2 and NA in the first row of the output. In statistics, this is called a contingency table.
There is one problem ...
Get The Art of R Programming now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.