Aliases: table summary.table print.summary.table as.data.frame.table as.table as.table.default is.table [.table
Keywords: category
### ** Examples require(stats) # for rpois and xtabs ## Simple frequency distribution table(rpois(100, 5))
1 2 3 4 5 6 7 8 9 10 11 3 7 14 12 21 19 13 4 5 1 1
## Check the design: with(warpbreaks, table(wool, tension))
tension wool L M H A 9 9 9 B 9 9 9
table(state.division, state.region)
state.region state.division Northeast South North Central West New England 6 0 0 0 Middle Atlantic 3 0 0 0 South Atlantic 0 8 0 0 East South Central 0 4 0 0 West South Central 0 4 0 0 East North Central 0 0 5 0 West North Central 0 0 7 0 Mountain 0 0 0 8 Pacific 0 0 0 5
# simple two-way contingency table with(airquality, table(cut(Temp, quantile(Temp)), Month))
Month 5 6 7 8 9 (56,72] 24 3 0 1 10 (72,79] 5 15 2 9 10 (79,85] 1 7 19 7 5 (85,97] 0 5 10 14 5
a <- letters[1:3] table(a, sample(a)) # dnn is c("a", "")
a a b c a 0 1 0 b 1 0 0 c 0 0 1
table(a, sample(a), deparse.level = 0) # dnn is c("", "")
a b c a 0 1 0 b 1 0 0 c 0 0 1
table(a, sample(a), deparse.level = 2) # dnn is c("a", "sample(a)")
sample(a) a a b c a 1 0 0 b 0 1 0 c 0 0 1
## xtabs() <-> as.data.frame.table() : UCBAdmissions ## already a contingency table
, , Dept = A Gender Admit Male Female Admitted 512 89 Rejected 313 19 , , Dept = B Gender Admit Male Female Admitted 353 17 Rejected 207 8 , , Dept = C Gender Admit Male Female Admitted 120 202 Rejected 205 391 , , Dept = D Gender Admit Male Female Admitted 138 131 Rejected 279 244 , , Dept = E Gender Admit Male Female Admitted 53 94 Rejected 138 299 , , Dept = F Gender Admit Male Female Admitted 22 24 Rejected 351 317
DF <- as.data.frame(UCBAdmissions) class(tab <- xtabs(Freq ~ ., DF)) # xtabs & table
[1] "xtabs" "table"
## tab *is* "the same" as the original table: all(tab == UCBAdmissions)
[1] TRUE
all.equal(dimnames(tab), dimnames(UCBAdmissions))
[1] TRUE
a <- rep(c(NA, 1/0:3), 10) table(a) # does not report NA's
a 0.333333333333333 0.5 1 Inf 10 10 10 10
table(a, exclude = NULL) # reports NA's
a 0.333333333333333 0.5 1 Inf 10 10 10 10 <NA> 10
b <- factor(rep(c("A","B","C"), 10)) table(b)
b A B C 10 10 10
table(b, exclude = "B")
b A C 10 10
d <- factor(rep(c("A","B","C"), 10), levels = c("A","B","C","D","E")) table(d, exclude = "B")
d A C D E 10 10 0 0
print(table(b, d), zero.print = ".")
d b A B C D E A 10 . . . . B . 10 . . . C . . 10 . .
## NA counting: is.na(d) <- 3:4 d. <- addNA(d) d.[1:7]
[1] A B <NA> <NA> B C A Levels: A B C D E <NA>
table(d.) # ", exclude = NULL" is not needed
d. A B C D E <NA> 9 10 9 0 0 2
## i.e., if you want to count the NA's of 'd', use table(d, useNA = "ifany")
d A B C D E <NA> 9 10 9 0 0 2
## "pathological" case: d.patho <- addNA(c(1,NA,1:2,1:3))[-7]; is.na(d.patho) <- 3:4 d.patho
[1] 1 <NA> <NA> <NA> 1 2 Levels: 1 2 3 <NA>
## just 3 consecutive NA's ? --- well, have *two* kinds of NAs here : as.integer(d.patho) # 1 4 NA NA 1 2
[1] 1 4 NA NA 1 2
## ## In R >= 3.4.0, table() allows to differentiate: table(d.patho) # counts the "unusual" NA
d.patho 1 2 3 <NA> 2 1 0 1
table(d.patho, useNA = "ifany") # counts all three
d.patho 1 2 3 <NA> 2 1 0 3
table(d.patho, exclude = NULL) # (ditto)
d.patho 1 2 3 <NA> 2 1 0 3
table(d.patho, exclude = NA) # counts none
d.patho 1 2 3 2 1 0
## Two-way tables with NA counts. The 3rd variant is absurd, but shows ## something that cannot be done using exclude or useNA. with(airquality, table(OzHi = Ozone > 80, Month, useNA = "ifany"))
Month OzHi 5 6 7 8 9 FALSE 25 9 20 19 27 TRUE 1 0 6 7 2 <NA> 5 21 5 5 1
with(airquality, table(OzHi = Ozone > 80, Month, useNA = "always"))
Month OzHi 5 6 7 8 9 <NA> FALSE 25 9 20 19 27 0 TRUE 1 0 6 7 2 0 <NA> 5 21 5 5 1 0
with(airquality, table(OzHi = Ozone > 80, addNA(Month)))
OzHi 5 6 7 8 9 <NA> FALSE 25 9 20 19 27 0 TRUE 1 0 6 7 2 0