Data Frames

Here is a built-in example data frame with 52 rows (we preview the first few):


In [8]:
head(warpbreaks)


Out[8]:
breakswooltension
126AL
230AL
354AL
425AL
570AL
652AL

Ignoring the "breaks" attribute, let's make a contingency table of counts. We're using the GLM syntax to describe which columns we want, nothing explained "~" by the wool and tension variables.


In [15]:
ct = xtabs(~wool+tension, data=warpbreaks)

In [16]:
ct


Out[16]:
    tension
wool L M H
   A 9 9 9
   B 9 9 9

In [23]:
is.table(ct)


Out[23]:
TRUE

We can also use the table function, which converts a dataframe into a tabke, and the margin.table function, which marginalizes a table, to accomplish this.


In [24]:
table(warpbreaks)


Out[24]:
, , tension = L

      wool
breaks A B
    10 0 0
    12 0 0
    13 0 0
    14 0 1
    15 0 0
    16 0 0
    17 0 0
    18 0 0
    19 0 1
    20 0 1
    21 0 0
    24 0 0
    25 1 0
    26 2 0
    27 0 1
    28 0 0
    29 0 2
    30 1 0
    31 0 1
    35 0 0
    36 0 0
    39 0 0
    41 0 1
    42 0 0
    43 0 0
    44 0 1
    51 1 0
    52 1 0
    54 1 0
    67 1 0
    70 1 0

, , tension = M

      wool
breaks A B
    10 0 0
    12 1 0
    13 0 0
    14 0 0
    15 0 0
    16 0 1
    17 1 0
    18 2 0
    19 0 1
    20 0 0
    21 1 1
    24 0 0
    25 0 0
    26 0 1
    27 0 0
    28 0 1
    29 1 1
    30 1 0
    31 0 0
    35 1 0
    36 1 0
    39 0 2
    41 0 0
    42 0 1
    43 0 0
    44 0 0
    51 0 0
    52 0 0
    54 0 0
    67 0 0
    70 0 0

, , tension = H

      wool
breaks A B
    10 1 0
    12 0 0
    13 0 1
    14 0 0
    15 1 2
    16 0 1
    17 0 1
    18 1 0
    19 0 0
    20 0 1
    21 1 1
    24 1 1
    25 0 0
    26 1 0
    27 0 0
    28 1 1
    29 0 0
    30 0 0
    31 0 0
    35 0 0
    36 1 0
    39 0 0
    41 0 0
    42 0 0
    43 1 0
    44 0 0
    51 0 0
    52 0 0
    54 0 0
    67 0 0
    70 0 0

In [30]:
margin.table(table(warpbreaks),c(2,3))


Out[30]:
    tension
wool L M H
   A 9 9 9
   B 9 9 9

In [ ]: