Dalgaard Chapter 4 Functions – Alex Crawford

4.5 Tables

4.5.1 Generating Tables

Create a matrix with the matrix() function. Tables values must be inserted as a single vector.
nrow= determines the number of rows.
ncol= determines the number of columns. (If you give both and they don't match, it will recycle values. DANGER!)
byrow=TRUE makes the entry by row instead of column. (In below example, first first entries are in the same row b/c byrow=T.)

caff.marital <- matrix(c(652, 1537, 598, 242, 36, 46, 38, 21, 218, 327, 106, 
    67), nrow = 3, byrow = T)
caff.marital
##      [,1] [,2] [,3] [,4]
## [1,]  652 1537  598  242
## [2,]   36   46   38   21
## [3,]  218  327  106   67

Adding column and row names:
colnames(MATRIX) <- c(DATA)
rownames(MATRIX) <- c(DATA)

colnames(caff.marital) <- c("0", "1-150", "151-300", ">300")
rownames(caff.marital) <- c("Married", "Prev.married", "Single")
caff.marital
##                0 1-150 151-300 >300
## Married      652  1537     598  242
## Prev.married  36    46      38   21
## Single       218   327     106   67

Adding headings for the rows and columns:
names(dimnames(MATRIX)) <- c(ROWSNAME,COLUMNSNAME)

names(dimnames(caff.marital)) <- c("marital", "consumption")
caff.marital
##               consumption
## marital          0 1-150 151-300 >300
##   Married      652  1537     598  242
##   Prev.married  36    46      38   21
##   Single       218   327     106   67

Converting a Matrix to a Table:
as.table(MATRIX)
Note: Must convert a matrix to a table before converting it to a data frame frequency chart – and must make this a double operation!

as.table(caff.marital)
##               consumption
## marital           0 1-150 151-300 >300
##   Married       652  1537     598  242
##   Prev.married   36    46      38   21
##   Single        218   327     106   67
as.data.frame(as.table(caff.marital))
##         marital consumption Freq
## 1       Married           0  652
## 2  Prev.married           0   36
## 3        Single           0  218
## 4       Married       1-150 1537
## 5  Prev.married       1-150   46
## 6        Single       1-150  327
## 7       Married     151-300  598
## 8  Prev.married     151-300   38
## 9        Single     151-300  106
## 10      Married        >300  242
## 11 Prev.married        >300   21
## 12       Single        >300   67

Creating a table from a data frame:
table(ROWS,COLUMNS) is simplest way
xtabs(~ROWS + COLUMNS, data=DATA.FRAME) is more complicated
ftable(COLUMNPRIMARY + COLUMNSECONDARY ~ ROWS, data=DATA.FRAME) is best for tables with more than 2 dimensions.

CO2 <- read.csv("/Users/telekineticturtle/Desktop/Colorado 13/Quant Methods/Data/co2_LAB1.csv")
table(CO2$month, CO2$site)
##     
##       0  1
##   1  38 38
##   2  39 39
##   3  39 39
##   4  39 39
##   5  39 39
##   6  39 39
##   7  39 39
##   8  39 39
##   9  39 39
##   10 39 39
##   11 39 39
##   12 39 39
xtabs(~month + site, data = CO2)
##      site
## month  0  1
##    1  38 38
##    2  39 39
##    3  39 39
##    4  39 39
##    5  39 39
##    6  39 39
##    7  39 39
##    8  39 39
##    9  39 39
##    10 39 39
##    11 39 39
##    12 39 39
ftable(month + site ~ year, data = CO2)
##      month 1   2   3   4   5   6   7   8   9   10   11   12  
##      site  0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1  0 1  0 1  0 1
## year                                                         
## 1969       0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1  1 1  1 1  1 1
## 1970       1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1  1 1  1 1  1 1
## 1971       1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1  1 1  1 1  1 1
## 1972       1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1  1 1  1 1  1 1
## 1973       1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1  1 1  1 1  1 1
## 1974       1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1  1 1  1 1  1 1
## 1975       1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1  1 1  1 1  1 1
## 1976       1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1  1 1  1 1  1 1
## 1977       1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1  1 1  1 1  1 1
## 1978       1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1  1 1  1 1  1 1
## 1979       1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1  1 1  1 1  1 1
## 1980       1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1  1 1  1 1  1 1
## 1981       1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1  1 1  1 1  1 1
## 1982       1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1  1 1  1 1  1 1
## 1983       1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1  1 1  1 1  1 1
## 1984       1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1  1 1  1 1  1 1
## 1985       1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1  1 1  1 1  1 1
## 1986       1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1  1 1  1 1  1 1
## 1987       1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1  1 1  1 1  1 1
## 1988       1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1  1 1  1 1  1 1
## 1989       1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1  1 1  1 1  1 1
## 1990       1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1  1 1  1 1  1 1
## 1991       1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1  1 1  1 1  1 1
## 1992       1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1  1 1  1 1  1 1
## 1993       1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1  1 1  1 1  1 1
## 1994       1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1  1 1  1 1  1 1
## 1995       1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1  1 1  1 1  1 1
## 1996       1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1  1 1  1 1  1 1
## 1997       1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1  1 1  1 1  1 1
## 1998       1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1  1 1  1 1  1 1
## 1999       1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1  1 1  1 1  1 1
## 2000       1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1  1 1  1 1  1 1
## 2001       1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1  1 1  1 1  1 1
## 2002       1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1  1 1  1 1  1 1
## 2003       1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1  1 1  1 1  1 1
## 2004       1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1  1 1  1 1  1 1
## 2005       1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1  1 1  1 1  1 1
## 2006       1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1  1 1  1 1  1 1
## 2007       1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1  1 1  1 1  1 1

4.5.2 Marignal Tables

To tabulate category frequency, use
margin.table(DATA,1) for rows and
margin.table(DATA,2) for columns

month.site <- table(CO2$month, CO2$sit)
margin.table(month.site, 1)
## 
##  1  2  3  4  5  6  7  8  9 10 11 12 
## 76 78 78 78 78 78 78 78 78 78 78 78
margin.table(month.site, 2)
## 
##   0   1 
## 467 467

Make a table of proportions with
prop.table(DATA,1) for by row and
prop.table(DATA,2) for by column
Multiply by 100 to get percentages.

prop.table(month.site, 1)
##     
##        0   1
##   1  0.5 0.5
##   2  0.5 0.5
##   3  0.5 0.5
##   4  0.5 0.5
##   5  0.5 0.5
##   6  0.5 0.5
##   7  0.5 0.5
##   8  0.5 0.5
##   9  0.5 0.5
##   10 0.5 0.5
##   11 0.5 0.5
##   12 0.5 0.5
prop.table(month.site, 2) * 100
##     
##          0     1
##   1  8.137 8.137
##   2  8.351 8.351
##   3  8.351 8.351
##   4  8.351 8.351
##   5  8.351 8.351
##   6  8.351 8.351
##   7  8.351 8.351
##   8  8.351 8.351
##   9  8.351 8.351
##   10 8.351 8.351
##   11 8.351 8.351
##   12 8.351 8.351

To get percentages for the entire population of the table, just divide each cell by the sum of all cells.

month.site/sum(month.site) * 100
##     
##          0     1
##   1  4.069 4.069
##   2  4.176 4.176
##   3  4.176 4.176
##   4  4.176 4.176
##   5  4.176 4.176
##   6  4.176 4.176
##   7  4.176 4.176
##   8  4.176 4.176
##   9  4.176 4.176
##   10 4.176 4.176
##   11 4.176 4.176
##   12 4.176 4.176