Chapter 5

Tables

Is it true that t is a table?

t <- table(c(1, 1, 2, 3, 3, 3))

Yes

because it is the result of a vector of values passed as an argument to the function table().


Is it true that t is a table, where t is

1 2 3 
2 1 3 

Yes

because it is a vector of count values labeled by the values being counted.


How many cases does t have where t is

table(c(1, 1, 2, 3, 3, 3))

6

because there are six values in the vector passed to table().


How many columns does t have where t is

table(c(1, 1, 2, 3, 3, 3))

3

because there are three unique values in the vector passed to table().


How many dimensions does t have where t is

table(c(1, 1, 2, 3, 3, 3))

3

because one-way table dimensions are the same as one-way table columns, and there are three unique values in the vector passed to table().


How many factors does t have where t is

table(c(1, 1, 2, 3, 3, 3))

1

because one vector of values has been passed to table().


Is it true that the number of cases and columns created by table(t) are equal where t is

table(c(1, 2, 3, 4))

Yes

because the values in t are all unique. Because columns and dimensions are the same, the number of cases are the same as the number of dimensions. With this specification of t, the values, cases, columns and dimensions are all equal.


What is t[1] where t is

table(c(1, 1, 2, 3, 3, 3))

2

because the values in a one-way table are a count, and the value of column 1, 1, appears twice in the factor passed to the table.


What is t[3] where t is

table(c(1, 1, 2, 3, 3, 3))

3

because the values in a one-way table are a count, and the value of column 3, 3, appears three times in the factor passed to the table.


Summary: One-way Tables

A one-way table is created by passing a vector of values to the table() function. The table function counts the number of time each unique value occurs and creates a one-dimensional array with the unique values serving as the column names. The number of dimensions in a one-way table is equal to the number of columns. The number of cases in the table is equal to the number of values in the vector passed in. The number of factors in a one-way table is one. You can use indexing (t[1]) to access the count data.


Is it true that fans is a table?

town <- c("Boston", "New York", "New York", "Boston", "Boston")
team <- c("Red Sox", "Yankees", "Yankees", "Yankees", "Red Sox")
fans <- table(town, team)

Yes

because it is two vectors passed as arguments to the function table().


Is it true that fans is a table where fans is

          team
town       Red Sox Yankees
  Boston         2       1
  New York       0       2

Yes

because it is a matrix of count values labelled by the values being counted.


How many factors does fans have where fans is

town <- c("Boston", "New York", "New York", "Boston", "Boston")
team <- c("Red Sox", "Yankees", "Yankees", "Yankees", "Red Sox")
table(town, team)

2

because two vectors of values have been passed as arguments to the function table().


How many cases does fans have where fans is

town <- c("Boston", "New York", "New York", "Boston", "Boston")
team <- c("Red Sox", "Yankees", "Yankees", "Yankees", "Red Sox")
table(town, team)

5

because the two vectors passed to table() each have five values.


How many dimensions does fans have where fans is

town <- c("Boston", "New York", "New York", "Boston", "Boston")
team <- c("Red Sox", "Yankees", "Yankees", "Yankees", "Red Sox")
table(town, team)

2 2

because the two vectors passed to table() each have two unique values. In a two-way table, the number of dimensions are equal to the number of columns and the number of rows.


What is fans[1, 2] where fans is

town <- c("Boston", "New York", "New York", "Boston", "Boston")
team <- c("Red Sox", "Yankees", "Yankees", "Yankees", "Red Sox")
table(town, team)

1

because the values in a two-way table are a count of the number of times a pair of values appear across the two factors passed to the table. [1, 2] specifies row one and column two:

          team
town       Red Sox Yankees
  Boston         2       1
  New York       0       2

It's worth noting that these are the values "Boston" and "Yankees". This pair appears once, in the fourth of five cases.


Is it true that fans is a table?

town <- c("Boston", "New York", "New York", "Boston", "Boston")
team <- c("Red Sox", "Yankees", "Yankees", "Yankees", "Red Sox")
year <- c("1996", "2004", "1996", "2004", "2004")
fans <- table(town, team, year)

Yes

because it is three vectors passed as arguments to the function table(). It looks like this:

, , year = 1996

         town
team      Boston New York
  Red Sox      1        0
  Yankees      0        1

, , year = 2004

         town
team      Boston New York
  Red Sox      1        0
  Yankees      1        1

How many dimensions does fans have where fans is

town <- c("Boston", "New York", "New York", "Boston", "Boston")
team <- c("Red Sox", "Yankees", "Yankees", "Yankees", "Red Sox")
year <- c("1996", "2004", "1996", "2004", "2004")
table(town, team, year)

2 2 2

because the three vectors passed to table() each have two unique values.


Is it true that t is a table?

a <- c(1, 2, 3, 4)
b <- c(1, 2, 3, 4, 5)
t <- table(a, b)

No

because factors passed to table() must be of equal lengths.


Is it true that t is a table?

t <- table(1, 1, 2, 3, 3, 3)

Yes

because it is six vectors passed as arguments to the function table(), and they are of equal lengths. However, it may not look like you expect:

, ,  = 2,  = 3,  = 3,  = 3

   
    1
  1 1

How many factors does t have where t is

table(1, 1, 2, 3, 3, 3)

Six

because there are six vectors passed as arguments to the function table().


How many cases does t have where t is

table(1, 1, 2, 3, 3, 3)

1

because each of the six vectors passed to the function table() has one value.


Summary: Two-way & Three-way Tables

Two-way tables can be created by passing two vectors of equal lengths to the table() function. Three-way, four-way, etc can similarly be created by passing three, four or more vectors to the table() function. The length of the vectors is equal to the total number of cases, and the count values will be generated based on how many cases have a particular set of values. The dimensions are the number of unique values in each vectors - for a two-way table, this is equivalent to the columns and rows. Matrix indexing (t[1, 2]) can be used to access the count data.

When you pass in individual numbers to table(), as opposed to a vector, it treats each number as a vector of length one. That means six individual numbers will be treated as a six-way table with one case, which is probably not what you're looking for.


Is it true that t is a table?

diy_table <- matrix(c(1, 2, 3))
t <- as.table(diy_table)

Yes

because it is a matrix of values passed into the function as.table().


How many columns does t have where t is

diy_table <- matrix(c(1, 2, 3))
as.table(diy_table)

1

because the matrix of values passed into the function as.table() has one column and three rows.


How many columns does t have where t is

diy_table <- matrix(c(1, 2, 3), ncol=3, byrow=TRUE)
as.table(diy_table)

3

because the matrix of values passed into the function as.table() has three columns and one row.


How many columns does t have where t is

diy_table <- matrix(c(1, 2, 2, 3, 3, 3), ncol=3, byrow=TRUE)
as.table(diy_table)

3

because the matrix of values passed into as.table() represent count data, not data to be counted, and the matrix of values passed into the function as.table() has three columns and three rows.


What is the name of the first column of t where t is

diy_table <- matrix(c(1, 2, 2, 3, 3, 3), ncol=3, byrow=TRUE)
as.table(diy_table)

A

because the default names for the rows and columns of matrices are A, B, C, etc.


What is the name of the first column of t where t is

diy_table <- matrix(c(1, 2, 3), ncol=3, byrow=TRUE)
colnames(diy_table) <- c("Do", "Re", "Mi")
as.table(diy_table)

"Do"

because colnames "Do", "Re", and "Mi" have been assigned to the matrix passed into the function as.table().


What is the name of the first column of t where t is

diy_table <- matrix(c(1, 2, 3), ncol=3, byrow=TRUE)
colnames(diy_table) <- c("Do", "Re", "Mi")
as.table(diy_table)

"Do"

because colnames "Do", "Re", and "Mi" have been assigned to the matrix passed into the function as.table().


Summary: Making Tables From Scratch

Tables can be generated from data using the table() function, or from scratch using the as.table() function. Data passed to the as.table() function are count data, not data to be counted. This count data should be passed in matrix format, including column and row names, which are useful but optional.