Sunday, June 28, 2020

table


The table command lets you build contingency tables (two-way table, cross tablulation, or crosstab). It is similar to xtabs. Both are depictions of spread, which display multivariate frequency distribution of two variables, though from the R documentation, it looks like you can use xtabs for more than two variables.

Wikipedia provides this example for a contingency table. Dominant handedness is broken down by gender:

Handedness
Sex
Right handed
Left handed
Total
Male
43
9
52
Female
44
4
48
Total
87
13
100

The red values are the column and row margin totals.

You see contingency tables everywhere. One of my stats classes focused very heavily on them, pumping nearly all the homework and class examples through Excel.

The table command has 9 parameters, of which you will often use row.names and responseName. The example below uses quantile(Temp) as row.names, and Month as the responseName.

R Example

This example from the R docs uses the build in dataset airquality, taking the Temp column from airquality, and creating a contingency table with a row grouping by quartile of temperature values, and a column grouping by month number.

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



The table reveals, for example, that when the value of the temperature is within the 1st quartile of temp values (between 56 and 72), the counts of when those temperatures occurred between May and September are:

Month
# of times the temperature was between 56 and 72
May
24
June
3
July
0
August
1
September
10

No comments:

Post a Comment

Please help to combat malicious use of the Internet.