Three-Way Tables in R

Three-Way Tables in R:- Three-way tables are essential tools in data analysis, allowing us to examine the relationship between three categorical variables.

In R, creating three-way tables is a straightforward process, thanks to the built-in functions xtabs() and ftable().

In this article, we will guide you through creating three-way tables using these functions, along with a practical example.

Causal Conclusions and Control of Confounding Variables (

Creating a Three-Way Table in R

Suppose we have a dataset containing information about basketball players. Our goal is to create a three-way table based on the variables ‘team’, ‘position’, and ‘starter’.

Here’s a step-by-step guide on how to achieve this using R.

Step 1: Create a Data Frame

First, we need to create a data frame with the given information. In our example, the data frame is named ‘df’.

#create data frame
df <- data.frame(team=c('A', 'A', 'A', 'A', 'A', 'B', 'B', 'B', 'B', 'B'),
                 position=c('G', 'G', 'G', 'F', 'F', 'G', 'G', 'F', 'F', 'F'),
                 starter=c('Yes', 'No', 'No', 'Yes', 'No',
                           'Yes', 'No', 'Yes', 'Yes', 'No'),
                 points=c(30, 28, 24, 24, 28, 14, 16, 20, 34, 29))

Step 2: View the Data Frame

To ensure that the data frame has been created correctly, we can view its contents.

#view data frame
   team position starter points
1     A        G     Yes    130
2     A        G      No    218
3     A        G      No    214
4     A        G     Yes    224
5     A        F     Yes    238
6     A        G     Yes    164
7     B        G      No    166
8     B        F     Yes    120
9     B        F     Yes    234
10    B        F     Yes    249

Step 3: Create a Three-Way Table Using xtabs()

Now, we can create a three-way table using the xtabs() function. This function takes the formula ‘~ team + position + starter’ and the data frame ‘df’ as arguments.

#create three-way table
three_way <- xtabs(~ team + position + starter, data=df)

Step 4: View the Three-Way Table

To view the three-way table, we simply use the table name we just created.

#view three-way table

    , , starter = No

team F G
   A 1 2
   B 1 1

, , starter = Yes

team F G
   A 1 1
   B 2 1

The output will display two tables, one for each level of the ‘starter’ variable.

Step 5: Convert the Table to a Flat Format Using ftable() (Optional)

If you’d like to view the three-way table in a flattened format, you can use the ftable() function.

#convert table to ftable
three_way_ftable <- ftable(three_way)

#view ftable

       starter No Yes
team position               
A    F                 1   1
     G                 2   1
B    F                 1   2
     G                 1   1

The resulting three-way table will show the frequencies of all three variables in a single, “flat” format.


In this article, we have demonstrated how to create a three-way table in R using the xtabs() and ftable() functions.

By following these steps, you can easily analyze the relationship between three categorical variables in your dataset.

This powerful tool can provide valuable insights into your data and help you make informed decisions.

Data Science for Business: Fundamentals of Analysis (

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

fifteen + 7 =