How can a two way table be created in R, and what are some examples of its use? 2

How can a two way table be created in R, and what are some examples of its use?

A two way table in R is a data structure that displays the relationship between two categorical variables. It is created by using the “table” function in R, which tabulates the count or frequency of the observations for each combination of the two variables.

One example of its use is in market research, where a two way table can be used to analyze the relationship between demographics (such as age, gender, or income) and consumer behavior (such as purchase frequency or preferred product). It can also be used in social sciences to examine the relationship between two categorical variables, such as education level and political affiliation.

In addition, a two way table can also be used to identify patterns and trends in data, such as determining which combinations of variables are most common or have the highest frequency. It can also be used for data visualization, as the table can be converted into a graphical representation, such as a bar chart or heat map. Overall, a two way table is a useful tool for organizing and analyzing categorical data in R.

Create a Two Way Table in R (With Examples)


A two-way table is a type of table that displays the frequencies for two categorical variables.

For example, the following two-way table shows the results of a survey that asked 100 people which sport they liked best: baseball, basketball, or football.

The rows display the gender of the respondent and the columns show which sport they chose:

This tutorial provides several examples of how to create and work with two-way tables in R.

Example 1: Create a Two Way Table from Scratch

The following code shows how to create a two way table from scratch using the as.table() function:

#create matrix
data <- matrix(c(13, 23, 15, 16, 20, 13), ncol=3)

#specify row and column names of matrix
rownames(data) <- c('Male', 'Female')
colnames(data) <- c('Baseball', 'Basketball', 'Football')#convert matrix to table
data <- as.table(data)

#display table
data

       Baseball Basketball Football
Male         13         15       20
Female       23         16       13

Example 2: Create a Two Way Table from Data

The following code shows how to create a two-way table from a data frame:

#create data frame
df <- data.frame(sport=c('Base', 'Base', 'Bask', 'Foot', 'Foot'),
                 gender=c('Male', 'Female', 'Male', 'Male', 'Female'))

#view data frame 
df

#create two way table from data frame
data <- table(df$gender, df$sport)

#display two way table
data 

         Base Bask Foot
  Female    1    0    1
  Male      1    1    1

Example 3: Calculate Margin Sums of a Two Way Table

The following code shows how to calculate margin sums of a two-way table using the margin.table() function:

#create matrix of data
data <- matrix(c(13, 15, 20, 23, 16, 13), ncol=3)
rownames(data) <- c('Male', 'Female')
colnames(data) <- c('Baseball', 'Basketball', 'Football')

#find sum of genders
margin.table(data, margin=1)

  Male Female 
    49     51

#find sum of sports

margin.table(data, margin=2)
  Baseball Basketball   Football 
        28         43         29

Example 4: Visualize Two Way Table Frequencies

One way to visualize the frequencies in a two way table is to create a barplot:

barplot(data, legend=True, beside=True, main='Favorite Sport by Gender')

Another way to visualize the frequencies in a two way table is to create a mosaic plot:

mosaicplot(data, main='Sports Preferences', xlab='Gender', ylab='Favorite Sport')


You can find more R tutorials on .

Cite this article

stats writer (2024). How can a two way table be created in R, and what are some examples of its use?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-a-two-way-table-be-created-in-r-and-what-are-some-examples-of-its-use/

stats writer. "How can a two way table be created in R, and what are some examples of its use?." PSYCHOLOGICAL SCALES, 27 Apr. 2024, https://scales.arabpsychology.com/stats/how-can-a-two-way-table-be-created-in-r-and-what-are-some-examples-of-its-use/.

stats writer. "How can a two way table be created in R, and what are some examples of its use?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-a-two-way-table-be-created-in-r-and-what-are-some-examples-of-its-use/.

stats writer (2024) 'How can a two way table be created in R, and what are some examples of its use?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-a-two-way-table-be-created-in-r-and-what-are-some-examples-of-its-use/.

[1] stats writer, "How can a two way table be created in R, and what are some examples of its use?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, April, 2024.

stats writer. How can a two way table be created in R, and what are some examples of its use?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
PDF
Scroll to Top