Making Two-Way Tables in R

Here are three different ways to make two-way tables in R, depending on how your data is stored.

Method 1 - Directly Input Counts into a Matrix

self.image.matrix = matrix(c(116,274,1175,1469,1730,1112),ncol=2,byrow=T)
colnames(self.image.matrix)=c('Female','Male')
rownames(self.image.matrix)=c('Underweight','About Right','Overweight')
self.image.matrix
##             Female Male
## Underweight    116  274
## About Right   1175 1469
## Overweight    1730 1112
class(self.image.matrix)
## [1] "matrix"

Here the variable self.image is a matrix not a table, but R functions like chisq.test() that apply to tables usually also apply to matrices. This is usually the easiest method if you are directly inputing the table into R.

Method 2 - Converting a Data Frame to a Table

This is the method when your data is in a data.frame where every row represents one individual. As long as the data.frame only has two columns, the table() function will directly convert it to a two-way table. In this example, the data is stored in a comma separated values (csv) file with over 5000 rows. This is the standard method if your data is imported from a spreadsheet.

self.image.dataframe1 = read.csv('SelfImage.csv')
head(self.image.dataframe1)
##   gender  self.image
## 1 Female Underweight
## 2 Female Underweight
## 3 Female Underweight
## 4 Female Underweight
## 5 Female Underweight
## 6 Female Underweight
dim(self.image.dataframe1)
## [1] 5876    2
self.image.table1=table(self.image.dataframe1)
self.image.table1
##         self.image
## gender   About Right Overweight Underweight
##   Female        1175       1730         116
##   Male          1469       1112         274

Method 3 - Converting a Data Frame with Counts to a Table

This method applies when you have data stored in a data.frame with a separate column for the counts. Here the key function is the xtabs() function. The syntax for xtabs() is a little different, but it is similar to the syntax for linear regression and ANOVA.

gender = c('Female','Male')
self.image = c('Underweight','Underweight','About Right','About Right','Overweight','Overweight')
count = c(116,274,1175,1469,1730,1112)
self.image.dataframe2 = data.frame(gender,self.image,count)
self.image.dataframe2
##   gender  self.image count
## 1 Female Underweight   116
## 2   Male Underweight   274
## 3 Female About Right  1175
## 4   Male About Right  1469
## 5 Female  Overweight  1730
## 6   Male  Overweight  1112

Here the variable self.image.dataframe contains an extra column with the counts. So each row does not represent one individual, instead it represents all of the individuals in one cell of a two-way table. Here is how to use the xtabs() function:

self.image.table2 = xtabs(count ~ gender+self.image, data = self.image.dataframe2)
self.image.table2
##         self.image
## gender   About Right Overweight Underweight
##   Female        1175       1730         116
##   Male          1469       1112         274