| Line 69... |
Line 69... |
| 69 |
arguments in \code{\dots} are named, those names are used. For the
|
69 |
arguments in \code{\dots} are named, those names are used. For the
|
| 70 |
remaining arguments, \code{deparse.level = 0} gives an empty name,
|
70 |
remaining arguments, \code{deparse.level = 0} gives an empty name,
|
| 71 |
\code{deparse.level = 1} uses the supplied argument if it is a symbol,
|
71 |
\code{deparse.level = 1} uses the supplied argument if it is a symbol,
|
| 72 |
and \code{deparse.level = 2} will deparse the argument.
|
72 |
and \code{deparse.level = 2} will deparse the argument.
|
| 73 |
|
73 |
|
| 74 |
Only when \code{exclude} is specified and non-NULL (i.e., not by
|
74 |
Only when \code{exclude} is specified (i.e., not by default) and
|
| 75 |
default), will \code{table} potentially drop levels of factor
|
75 |
non-empty, will \code{table} potentially drop levels of factor
|
| 76 |
arguments.
|
76 |
arguments.
|
| 77 |
|
77 |
|
| 78 |
\code{useNA} controls if the table includes counts of \code{NA}
|
78 |
\code{useNA} controls if the table includes counts of \code{NA}
|
| 79 |
values: the allowed values correspond to never, only if the count is
|
79 |
values: the allowed values correspond to never (\code{"no"}), only if the count is
|
| 80 |
positive and even for zero counts. This is overridden by specifying
|
80 |
positive (\code{"ifany"}) and even for zero counts (\code{"always"}).
|
| - |
|
81 |
Note the somewhat \dQuote{pathological} case of two different kinds of
|
| 81 |
\code{exclude = NULL}. Note that levels specified in \code{exclude}
|
82 |
\code{NA}s which are treated differently, depending on both
|
| 82 |
are mapped to \code{NA} and so included in \code{NA} counts.
|
83 |
\code{useNA} and \code{exclude}, see \code{d.patho} in the
|
| - |
|
84 |
\sQuote{Examples:} below.
|
| 83 |
|
85 |
|
| 84 |
Both \code{exclude} and \code{useNA} operate on an "all or none"
|
86 |
Both \code{exclude} and \code{useNA} operate on an \dQuote{all or none}
|
| 85 |
basis. If you want to control the dimensions of a multiway table
|
87 |
basis. If you want to control the dimensions of a multiway table
|
| 86 |
separately, modify each argument using \code{\link{factor}} or
|
88 |
separately, modify each argument using \code{\link{factor}} or
|
| 87 |
\code{\link{addNA}}.
|
89 |
\code{\link{addNA}}.
|
| 88 |
|
90 |
|
| 89 |
It is best to supply factors rather than rely on coercion. In
|
91 |
Non-factor arguments \code{a} are coerced via \code{factor(a,
|
| 90 |
particular, \code{exclude} will be used in coercion to a factor, and
|
92 |
exclude=exclude)}. Since \R 3.4.0, care is taken \emph{not} to
|
| 91 |
so values (not levels) which appear in \code{exclude} before coercion
|
93 |
count the excluded values (where they were included in the \code{NA}
|
| 92 |
will be mapped to \code{NA} rather than be discarded.
|
94 |
count, previously).
|
| 93 |
|
95 |
|
| 94 |
The \code{summary} method for class \code{"table"} (used for objects
|
96 |
The \code{summary} method for class \code{"table"} (used for objects
|
| 95 |
created by \code{table} or \code{\link{xtabs}}) which gives basic
|
97 |
created by \code{table} or \code{\link{xtabs}}) which gives basic
|
| 96 |
information and performs a chi-squared test for independence of
|
98 |
information and performs a chi-squared test for independence of
|
| 97 |
factors (note that the function \code{\link{chisq.test}} currently
|
99 |
factors (note that the function \code{\link{chisq.test}} currently
|
| 98 |
only handles 2-d tables).
|
100 |
only handles 2-d tables).
|
| 99 |
}
|
101 |
}
|
| 100 |
\value{
|
102 |
\value{
|
| 101 |
\code{table()} returns a \emph{contingency table}, an object of
|
103 |
\code{table()} returns a \emph{contingency table}, an object of
|
| 102 |
class \code{"table"}, an array of integer values.
|
104 |
class \code{"table"}, an array of integer values.
|
| 103 |
Note that unlike S the result is always an array, a 1D array if one
|
105 |
Note that unlike S the result is always an \code{\link{array}}, a 1D
|
| 104 |
factor is given.
|
106 |
array if one factor is given.
|
| 105 |
|
107 |
|
| 106 |
\code{as.table} and \code{is.table} coerce to and test for contingency
|
108 |
\code{as.table} and \code{is.table} coerce to and test for contingency
|
| 107 |
table, respectively.
|
109 |
table, respectively.
|
| 108 |
|
110 |
|
| 109 |
The \code{as.data.frame} method for objects inheriting from class
|
111 |
The \code{as.data.frame} method for objects inheriting from class
|
| Line 118... |
Line 120... |
| 118 |
|
120 |
|
| 119 |
Use \code{\link{ftable}} for printing (and more) of
|
121 |
Use \code{\link{ftable}} for printing (and more) of
|
| 120 |
multidimensional tables. \code{\link{margin.table}},
|
122 |
multidimensional tables. \code{\link{margin.table}},
|
| 121 |
\code{\link{prop.table}}, \code{\link{addmargins}}.
|
123 |
\code{\link{prop.table}}, \code{\link{addmargins}}.
|
| 122 |
|
124 |
|
| - |
|
125 |
\code{\link{addNA}} for constructing factors with \code{\link{NA}} as
|
| - |
|
126 |
a level.
|
| - |
|
127 |
|
| 123 |
\code{\link{xtabs}} for cross tabulation of data frames with a
|
128 |
\code{\link{xtabs}} for cross tabulation of data frames with a
|
| 124 |
formula interface.
|
129 |
formula interface.
|
| 125 |
}
|
130 |
}
|
| 126 |
\references{
|
131 |
\references{
|
| 127 |
Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988)
|
132 |
Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988)
|
| Line 151... |
Line 156... |
| 151 |
## tab *is* "the same" as the original table:
|
156 |
## tab *is* "the same" as the original table:
|
| 152 |
all(tab == UCBAdmissions)
|
157 |
all(tab == UCBAdmissions)
|
| 153 |
all.equal(dimnames(tab), dimnames(UCBAdmissions))
|
158 |
all.equal(dimnames(tab), dimnames(UCBAdmissions))
|
| 154 |
|
159 |
|
| 155 |
a <- rep(c(NA, 1/0:3), 10)
|
160 |
a <- rep(c(NA, 1/0:3), 10)
|
| 156 |
table(a)
|
161 |
table(a) # does not report NA's
|
| 157 |
table(a, exclude = NULL)
|
162 |
table(a, exclude = NULL) # reports NA's
|
| 158 |
b <- factor(rep(c("A","B","C"), 10))
|
163 |
b <- factor(rep(c("A","B","C"), 10))
|
| 159 |
table(b)
|
164 |
table(b)
|
| 160 |
table(b, exclude = "B")
|
165 |
table(b, exclude = "B")
|
| 161 |
d <- factor(rep(c("A","B","C"), 10), levels = c("A","B","C","D","E"))
|
166 |
d <- factor(rep(c("A","B","C"), 10), levels = c("A","B","C","D","E"))
|
| 162 |
table(d, exclude = "B")
|
167 |
table(d, exclude = "B")
|
| Line 168... |
Line 173... |
| 168 |
d.[1:7]
|
173 |
d.[1:7]
|
| 169 |
table(d.) # ", exclude = NULL" is not needed
|
174 |
table(d.) # ", exclude = NULL" is not needed
|
| 170 |
## i.e., if you want to count the NA's of 'd', use
|
175 |
## i.e., if you want to count the NA's of 'd', use
|
| 171 |
table(d, useNA = "ifany")
|
176 |
table(d, useNA = "ifany")
|
| 172 |
|
177 |
|
| - |
|
178 |
## "pathological" case:
|
| - |
|
179 |
d.patho <- addNA(c(1,NA,1:2,1:3))[-7]; is.na(d.patho) <- 3:4
|
| - |
|
180 |
d.patho
|
| - |
|
181 |
## just 3 consecutive NA's ? --- well, have *two* kinds of NAs here :
|
| - |
|
182 |
as.integer(d.patho) # 1 4 NA NA 1 2
|
| - |
|
183 |
##
|
| - |
|
184 |
## In R >= 3.4.0, table() allows to differentiate:
|
| - |
|
185 |
table(d.patho) # counts the "unusual" NA
|
| - |
|
186 |
table(d.patho, useNA = "ifany") # counts all three
|
| - |
|
187 |
table(d.patho, exclude = NULL) # (ditto)
|
| - |
|
188 |
table(d.patho, exclude = NA) # counts none
|
| - |
|
189 |
|
| 173 |
## Two-way tables with NA counts. The 3rd variant is absurd, but shows
|
190 |
## Two-way tables with NA counts. The 3rd variant is absurd, but shows
|
| 174 |
## something that cannot be done using exclude or useNA.
|
191 |
## something that cannot be done using exclude or useNA.
|
| 175 |
with(airquality,
|
192 |
with(airquality,
|
| 176 |
table(OzHi = Ozone > 80, Month, useNA = "ifany"))
|
193 |
table(OzHi = Ozone > 80, Month, useNA = "ifany"))
|
| 177 |
with(airquality,
|
194 |
with(airquality,
|