EVOLUTION-MANAGER
Edit File: table.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title>R: Cross Tabulation and Table Creation</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <link rel="stylesheet" type="text/css" href="R.css" /> </head><body> <table width="100%" summary="page for table {base}"><tr><td>table {base}</td><td style="text-align: right;">R Documentation</td></tr></table> <h2>Cross Tabulation and Table Creation</h2> <h3>Description</h3> <p><code>table</code> uses the cross-classifying factors to build a contingency table of the counts at each combination of factor levels. </p> <h3>Usage</h3> <pre> table(..., exclude = if (useNA == "no") c(NA, NaN), useNA = c("no", "ifany", "always"), dnn = list.names(...), deparse.level = 1) as.table(x, ...) is.table(x) ## S3 method for class 'table' as.data.frame(x, row.names = NULL, ..., responseName = "Freq", stringsAsFactors = TRUE, sep = "", base = list(LETTERS)) </pre> <h3>Arguments</h3> <table summary="R argblock"> <tr valign="top"><td><code>...</code></td> <td> <p>one or more objects which can be interpreted as factors (including character strings), or a list (or data frame) whose components can be so interpreted. (For <code>as.table</code>, arguments passed to specific methods; for <code>as.data.frame</code>, unused.)</p> </td></tr> <tr valign="top"><td><code>exclude</code></td> <td> <p>levels to remove for all factors in <code>...</code>. If it does not contain <code><a href="NA.html">NA</a></code> and <code>useNA</code> is not specified, it implies <code>useNA = "ifany"</code>. See ‘Details’ for its interpretation for non-factor arguments.</p> </td></tr> <tr valign="top"><td><code>useNA</code></td> <td> <p>whether to include <code>NA</code> values in the table. See ‘Details’. Can be abbreviated.</p> </td></tr> <tr valign="top"><td><code>dnn</code></td> <td> <p>the names to be given to the dimensions in the result (the <em>dimnames names</em>).</p> </td></tr> <tr valign="top"><td><code>deparse.level</code></td> <td> <p>controls how the default <code>dnn</code> is constructed. See ‘Details’.</p> </td></tr> <tr valign="top"><td><code>x</code></td> <td> <p>an arbitrary <span style="font-family: Courier New, Courier; color: #666666;"><b>R</b></span> object, or an object inheriting from class <code>"table"</code> for the <code>as.data.frame</code> method. Note that <code>as.data.frame.table(x, *)</code> may be called explicitly for non-table <code>x</code> for “reshaping” <code><a href="array.html">array</a></code>s.</p> </td></tr> <tr valign="top"><td><code>row.names</code></td> <td> <p>a character vector giving the row names for the data frame.</p> </td></tr> <tr valign="top"><td><code>responseName</code></td> <td> <p>The name to be used for the column of table entries, usually counts.</p> </td></tr> <tr valign="top"><td><code>stringsAsFactors</code></td> <td> <p>logical: should the classifying factors be returned as factors (the default) or character vectors?</p> </td></tr> <tr valign="top"><td><code>sep, base</code></td> <td> <p>passed to <code><a href="dimnames.html">provideDimnames</a></code>.</p> </td></tr> </table> <h3>Details</h3> <p>If the argument <code>dnn</code> is not supplied, the internal function <code>list.names</code> is called to compute the ‘dimname names’. If the arguments in <code>...</code> are named, those names are used. For the remaining arguments, <code>deparse.level = 0</code> gives an empty name, <code>deparse.level = 1</code> uses the supplied argument if it is a symbol, and <code>deparse.level = 2</code> will deparse the argument. </p> <p>Only when <code>exclude</code> is specified (i.e., not by default) and non-empty, will <code>table</code> potentially drop levels of factor arguments. </p> <p><code>useNA</code> controls if the table includes counts of <code>NA</code> values: the allowed values correspond to never (<code>"no"</code>), only if the count is positive (<code>"ifany"</code>) and even for zero counts (<code>"always"</code>). Note the somewhat “pathological” case of two different kinds of <code>NA</code>s which are treated differently, depending on both <code>useNA</code> and <code>exclude</code>, see <code>d.patho</code> in the ‘Examples:’ below. </p> <p>Both <code>exclude</code> and <code>useNA</code> operate on an “all or none” basis. If you want to control the dimensions of a multiway table separately, modify each argument using <code><a href="factor.html">factor</a></code> or <code><a href="factor.html">addNA</a></code>. </p> <p>Non-factor arguments <code>a</code> are coerced via <code>factor(a, exclude=exclude)</code>. Since <span style="font-family: Courier New, Courier; color: #666666;"><b>R</b></span> 3.4.0, care is taken <em>not</em> to count the excluded values (where they were included in the <code>NA</code> count, previously). </p> <p>The <code>summary</code> method for class <code>"table"</code> (used for objects created by <code>table</code> or <code><a href="../../stats/html/xtabs.html">xtabs</a></code>) which gives basic information and performs a chi-squared test for independence of factors (note that the function <code><a href="../../stats/html/chisq.test.html">chisq.test</a></code> currently only handles 2-d tables). </p> <h3>Value</h3> <p><code>table()</code> returns a <em>contingency table</em>, an object of class <code>"table"</code>, an array of integer values. Note that unlike S the result is always an <code><a href="array.html">array</a></code>, a 1D array if one factor is given. </p> <p><code>as.table</code> and <code>is.table</code> coerce to and test for contingency table, respectively. </p> <p>The <code>as.data.frame</code> method for objects inheriting from class <code>"table"</code> can be used to convert the array-based representation of a contingency table to a data frame containing the classifying factors and the corresponding entries (the latter as component named by <code>responseName</code>). This is the inverse of <code><a href="../../stats/html/xtabs.html">xtabs</a></code>. </p> <h3>References</h3> <p>Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988) <em>The New S Language</em>. Wadsworth & Brooks/Cole. </p> <h3>See Also</h3> <p><code><a href="tabulate.html">tabulate</a></code> is the underlying function and allows finer control. </p> <p>Use <code><a href="../../stats/html/ftable.html">ftable</a></code> for printing (and more) of multidimensional tables. <code><a href="margin.table.html">margin.table</a></code>, <code><a href="prop.table.html">prop.table</a></code>, <code><a href="../../stats/html/addmargins.html">addmargins</a></code>. </p> <p><code><a href="factor.html">addNA</a></code> for constructing factors with <code><a href="NA.html">NA</a></code> as a level. </p> <p><code><a href="../../stats/html/xtabs.html">xtabs</a></code> for cross tabulation of data frames with a formula interface. </p> <h3>Examples</h3> <pre> require(stats) # for rpois and xtabs ## Simple frequency distribution table(rpois(100, 5)) ## Check the design: with(warpbreaks, table(wool, tension)) table(state.division, state.region) # simple two-way contingency table with(airquality, table(cut(Temp, quantile(Temp)), Month)) a <- letters[1:3] table(a, sample(a)) # dnn is c("a", "") table(a, sample(a), deparse.level = 0) # dnn is c("", "") table(a, sample(a), deparse.level = 2) # dnn is c("a", "sample(a)") ## xtabs() <-> as.data.frame.table() : UCBAdmissions ## already a contingency table DF <- as.data.frame(UCBAdmissions) class(tab <- xtabs(Freq ~ ., DF)) # xtabs & table ## tab *is* "the same" as the original table: all(tab == UCBAdmissions) all.equal(dimnames(tab), dimnames(UCBAdmissions)) a <- rep(c(NA, 1/0:3), 10) table(a) # does not report NA's table(a, exclude = NULL) # reports NA's b <- factor(rep(c("A","B","C"), 10)) table(b) table(b, exclude = "B") d <- factor(rep(c("A","B","C"), 10), levels = c("A","B","C","D","E")) table(d, exclude = "B") print(table(b, d), zero.print = ".") ## NA counting: is.na(d) <- 3:4 d. <- addNA(d) d.[1:7] table(d.) # ", exclude = NULL" is not needed ## i.e., if you want to count the NA's of 'd', use table(d, useNA = "ifany") ## "pathological" case: d.patho <- addNA(c(1,NA,1:2,1:3))[-7]; is.na(d.patho) <- 3:4 d.patho ## just 3 consecutive NA's ? --- well, have *two* kinds of NAs here : as.integer(d.patho) # 1 4 NA NA 1 2 ## ## In R >= 3.4.0, table() allows to differentiate: table(d.patho) # counts the "unusual" NA table(d.patho, useNA = "ifany") # counts all three table(d.patho, exclude = NULL) # (ditto) table(d.patho, exclude = NA) # counts none ## 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")) with(airquality, table(OzHi = Ozone > 80, Month, useNA = "always")) with(airquality, table(OzHi = Ozone > 80, addNA(Month))) </pre> <hr /><div style="text-align: center;">[Package <em>base</em> version 3.6.0 <a href="00Index.html">Index</a>]</div> </body></html>