# How to Create Count Tables and Contingency Tables in R

### 05.05.2021

When dealing with one more more factor, it is often needed to view the count of observations for each category. For example, if you are doing a survey of people and ask if they are happy or sad, you would like to know how many people said happy or sad. In this article, we will learn how to create Count and Contingency Tables in R.

Let's simulate an example class survey. We are going to ask students if they enjoyed the class and also note whether or not they passed the class. This way we can my some hypothesis on if there is a correlation between the two.

We start with a random sample of users saying yes for enjoyed class, and no if not. We can use the `table` function to create a count table.

``````enj.sample = sample(c("Yes", "No"), 20, replace = TRUE)
enjoyed = factor(enj.sample)

table(enjoyed)

# enjoyed
#  No Yes
#   8  12 ``````

Above you can see that we have 8 students said No and 12 said Yes.

Let's also simulate reults of pass and value. Noticed we generate another 20 samples to ensure we have the same 20 student above.

``````pass.sample = sample(c("Pass", "Fail"), 20, replace = TRUE)
passed = factor(pass.sample)

table(passed)

# passed
# Fail Pass
#   12    8 ``````

Again we use `table` to get our count table. We have 12 failed and 8 pass.

Now, we want to build a contingency table to see cobinations of the above factors. For example, in the table below we can see that 5 students Failed the class and said they did not enjoy the class. To create this table, we again use the `table` method, but we pass two factors.

``````table(enjoyed, passed)

#        passed
# enjoyed Fail Pass
#     No     5    3
#     Yes    7    5``````