Tables in R with table() and prop.table() (2024)

HOME DATA MANIPULATION IN R TABLE R

Data Manipulation in R Data transformation

Tables in R with table() and prop.table() (1)

The table function can be used for summarizing categorical data and generating absolute frequency and contigency tables. In this tutorial we will be exploring its syntax, various arguments, and practical examples to illustrate its utility in analyzing data. We will also explore the prop.table for relative frequency tables, xtabs for cross-tabulation, and addmargins to add margins to tables.

The table function

The table function in R is used to tabulate categorical data, counting the number of occurrences of each category. This function can create one-way tables, which provide the frequency of each category in a single variable, and two-way tables (or higher in high-dimensional arrays), which display the frequency distribution across two or more variables.

Syntax of the table function

The syntax of the function is the following:

table(..., exclude = if (useNA == "no") c(NA, NaN), useNA = c("no", "ifany", "always"), dnn = list.names(...), deparse.level = 1)

Being:

  • ...: one or more categorical variables or expressions to be tabulated.
  • exclude: optional argument specifying levels to be excluded from the table.
  • useNA: treatment of missing values. Possible options are "no" (default), "ifany" and "always"
  • dnn: character vector providing names for the resulting table.
  • deparse.level: controls how dnn is constructed by default. Read the documentation of the function for additional details.

One-way frequency tables

One-way tables represent the frequency distribution of a single variable. They are useful for understanding the distribution of categories within a variable. In order to create a table you will need to input a character vector, as illustrated below:

# Sample datadata <- c("B", "A", "C", "C", "A", "C", "B")# Create a simple frequency table for a categorical variableone_way_table <- table(data)one_way_table
dataA B C 2 2 3 

The previous ouput means that there are two elements that correspond to category "A", two correspond to "B" and three to "C". The previous table can be represented using a bar plot:

# Sample datadata <- c("B", "A", "C", "C", "A", "C", "B")# Create a simple frequency table for a categorical variableone_way_table <- table(data)# Plot the tablebarplot(one_way_table, col = 2:4, ylab = "Count")

Tables in R with table() and prop.table() (2)

The function provides an argument named exclude that can be used to exclude some categories from the output table. In the following example we are excluding "B".

# Sample datadata <- c("B", "A", "C", "C", "A", "C", "B")# Exclude specific levels from the frequency tabletable(data, exclude = "B")
dataA C 2 3

Sometimes, analyzing the presence of missing values is as important as the available data. The useNA argument can be leveraged to include NA values in the table. When set to "ifany" the table will also count the number of missing values.

# Sample datadata <- c("B", "A", NA, NA, "A", "C", "B")# Count NA values if anytable(data, useNA = "ifany")
data A B C <NA> 2 2 1 2 

When set to "always" the table will display the number of NA values even if there were none. This is very useful for data checking.

# Sample datadata <- c("B", "A", "A", "C", "B")# Count NA values even if there are nonetable(data, useNA = "always")
data A B C <NA> 2 2 1 0 

Two-way contingency tables

Two-way tables show the relationship between two categorical variables. They are crucial for examining the interactions between variables. This type of table can also be created with the table function, but you will need to input two character vectors of the same length instead of one, as illustrated below.

# Sample datagender <- c("Male", "Female", "Male", "Female", "Male")age_group <- c("Junior", "Senior", "Senior", "Junior", "Junior")# Create a two-way tabletwo_way_table <- table(gender, age_group)# View the tabletwo_way_table
 age_groupgender Junior Senior Female 1 1 Male 2 1

The previous data can be represented with a bar plot making use of the barplot function or any other similar function:

# Sample datagender <- c("Male", "Female", "Male", "Female", "Male", "Female")age_group <- c("Junior", "Senior", "Senior", "Junior", "Junior", "Senior")# Create a two-way tabletwo_way_table <- table(gender, age_group)# Plot the tablebarplot(two_way_table, col = 2:3, beside = TRUE, ylab = "Count")legend("topright", legend = c("Female", "Male"), fill = 2:3)

Tables in R with table() and prop.table() (3)

The prop.table function

The prop.table function takes a table created with table and converts it into a relative frequency table, also known as proportion table.

# Sample datagender <- c("Male", "Female", "Male", "Female", "Male")age_group <- c("Junior", "Senior", "Senior", "Junior", "Junior")# Create a two-way tabletwo_way_table <- table(gender, age_group)# Relative frequency tableprop.table(two_way_table)
 age_groupgender Junior Senior Female 0.2 0.2 Male 0.4 0.2

The function includes an argument named margin. Setting margin to 1 calculates proportions based on the sum of each row, whereas setting it to 2 calculates proportions based on the sum of each column.

# Sample datagender <- c("Male", "Female", "Male", "Female", "Male")age_group <- c("Junior", "Senior", "Senior", "Junior", "Junior")# Create a two-way tabletwo_way_table <- table(gender, age_group)# Relative frequency tableprop.table(two_way_table, margin = 1)
 age_groupgender Junior Senior Female 0.5000000 0.5000000 Male 0.6666667 0.3333333

The xtabs function

A function related to table is xtabs. The xtabs function allows creating contingency tables and it is specially useful for grouped data and when working with data frames. Unlike table, it uses a formula syntax, which allows for more complex specifications and is ideal for statistical analysis.

# Sample data framedf <- data.frame(x = c("G1", "G2", "G2", "G1", "G1", "G2"), y = c("A", "B", "B", "C", "A", "C"))# Contingency table with xtabstab <- xtabs(~ x + y, data = df)tab
 yx A B C G1 2 0 1 G2 0 2 1

An interesting feature of xtabs is that it can create weighted contingency tables. The following example illustrates how to input weights using the column w:

# Sample data framedf <- data.frame(x = c("G1", "G2", "G2", "G1", "G1", "G2"), y = c("A", "B", "B", "C", "A", "C"), w = c(0.1, 0.2, 0.2, 0.1, 0.1, 0.3))# Weighted contingency tabletab <- xtabs(w ~ x + y, data = df)tab
 yx A B C G1 0.2 0.0 0.1 G2 0.0 0.4 0.3

The addmargins function

The addmargins function in R is used to add row and/or column margins, usually representing sums or totals of the rows and/or columns to tables created with table or similar functions. The syntax of the function is the following:

addmargins(A, margin = NULL, FUN = sum, quiet = FALSE)

Being:

  • A: the input table.
  • margin: the desired margin. By default, the function calculates all margins, but when is set to 1, only row margins are calculated, and when set to 2, only column margins are calculated.
  • FUN: function to be applied to calculate the margins. It sums by default.
  • quiet: logical. If set to TRUE suppress messages.

When the function is applied to a table both margins will be added by default, counting the number of elements for rows and columns.

# Sample datagender <- c("Male", "Female", "Male", "Female", "Male")age_group <- c("Junior", "Senior", "Senior", "Junior", "Junior")# Create a two-way tabletwo_way_table <- table(gender, age_group)# Add marginsaddmargins(two_way_table)
 age_groupgender Junior Senior Sum Female 1 1 2 Male 2 1 3 Sum 3 2 5

However, if you only want to calculate the margins for rows or for columns you will need to set the margin argument to 1 or 2 depending on your needs.

# Sample datagender <- c("Male", "Female", "Male", "Female", "Male")age_group <- c("Junior", "Senior", "Senior", "Junior", "Junior")# Create a two-way tabletwo_way_table <- table(gender, age_group)# Add marginsaddmargins(two_way_table, margin = 2)
 age_groupgender Junior Senior Sum Female 1 1 2 Male 2 1 3

R version 4.3.2 (2023-10-31 ucrt)

Tables in R with table() and prop.table() (2024)

FAQs

What is the difference between table and prop table in R? ›

table is a function used to build a contingency table, which is a table that shows counts for categorical data, from one or more categories. prop. table is a function that accepts table output, returning proportions of the counts.

What does the table() function do in R? ›

The table() function in R is a versatile tool that allows you to create frequency tables, also known as contingency tables, from categorical data. Its primary purpose is to summarize and organize the counts or frequencies of different unique values present within a vector, factor, or column of a data frame.

What does the R function prop table () does in a crosstab? ›

Function prop. table() prints out the proportional values of the table that is given as an argument. We can make the table output more reader friendly by converting the proportional values into percentages (%).

How to create a table with two variables in R? ›

To create a two way table, simply pass two variables to the table() function instead of one. The output of a two-way table is a two-dimensional array of integers where the row names are set to the levels of the first variable and the column names are set to the levels of the second variable.

What does prop function do in R? ›

prop() calculates the proportion of a value or category in a variable.

What is the recommended number of props for a table display? ›

Explanation. The recommended number of props for a table display is typically between 1 to 3. This is because having too many props can make the table look cluttered and confusing, while too few can make it look sparse and uninteresting. Therefore, a balance of 1 to 3 props is usually the best approach.

How do you use a table function? ›

To invoke a user-defined table function, reference the function in the FROM clause of an SQL statement where it is to process a set of input values. The reference to the table function must be preceded by the TABLE clause and be contained in brackets.

What is the purpose or function of table? ›

A table is an item of furniture with a raised flat top and is supported most commonly by 1 to 4 legs (although some can have more). It is used as a surface for working at, eating from or on which to place things.

How to write a query to create a table? ›

SQL CREATE TABLE Statement
  1. CREATE TABLE table_name ( column1 datatype, column2 datatype, ...
  2. ExampleGet your own SQL Server. CREATE TABLE Persons ( PersonID int, ...
  3. CREATE TABLE new_table_name AS. SELECT column1, column2,... FROM existing_table_name. ...
  4. Example. CREATE TABLE TestTable AS. SELECT customername, contactname.

What is the prop test function in R? ›

prop. test can be used for testing the null that the proportions (probabilities of success) in several groups are the same, or that they equal certain given values.

How to make a 3 way table in R? ›

In this method, we use the xtabs() function to construct a three-way table. We specify the formula ~ var1 + var2 + var3 to indicate the three categorical variables of interest (e.g., Year, Study_Method, Result).

What is the purpose of the which () function in R? ›

Using the which() function with vectors

The which() in R returns the position of the values in the given input. You can also use the function to pass specific conditions and get the positions of the output that match the conditions as we saw in the last example.

What does a prop table do in R? ›

The prop. table() in R can be used to calculate the value of each cell in a (contingency) table as a proportion of all values.

How to use table() in R? ›

To use table(), simply add in the variables you want to tabulate separated by a comma. Note that table() does not have a data= argument like many other functions do (e.g., ggplot2 functions), so you much reference the variable using dataset$variable.

How do you combine two tables into one in R? ›

And joining by:
  1. Outer join - keep all rows of both tables (even if unmatching): all=T.
  2. Left join - all rows in the first table (even if not in the second): all. x=T.
  3. Right join - all rows in the second table (even if not in the first): all. y=T.
May 21, 2024

What is the difference between table and nested table? ›

A nested table is a data structure that represents a table within another table. It is embedded as a row into another table, creating a hierarchical or a “nested” structure. Nested tables allow you to extract data from tables with complicated structures where child-row data points inherit data points from parent rows.

What is the definition of prop table? ›

» PROPS TABLE. Definition: Table in convenient offstage area on which properties are prepared prior to a performance and to which they should be returned after use.

What is the difference between table and table schema? ›

Schema: Within a catalogue (or database), you have schemas. A schema is a collection of database objects, including tables, views, indexes, and procedures, grouped together. A schema is owned by a database user and shares the same name. Table: A table is the primary component of a schema.

What is the difference between table and Dataframe in R? ›

frame in R is similar to the data table which is used to create tabular data but data table provides a lot more features than the data frame so, generally, all prefer the data. table instead of the data. frame.

References

Top Articles
Today's top money market account rate roundup: Rates move lower — July 8, 2024
Best High-Yield Savings Accounts (up to 5.36%) of July 2024
Craigslist Myrtle Beach Motorcycles For Sale By Owner
San Angelo, Texas: eine Oase für Kunstliebhaber
Kathleen Hixson Leaked
Southeast Iowa Buy Sell Trade
Santa Clara College Confidential
CKS is only available in the UK | NICE
The Idol - watch tv show streaming online
Mr Tire Rockland Maine
Free Robux Without Downloading Apps
Texas (TX) Powerball - Winning Numbers & Results
R/Afkarena
Gwdonate Org
The fabulous trio of the Miller sisters
Echo & the Bunnymen - Lips Like Sugar Lyrics
Nebraska Furniture Tables
2024 U-Haul ® Truck Rental Review
Classic Lotto Payout Calculator
Cinebarre Drink Menu
Ups Access Point Lockers
Lcwc 911 Live Incident List Live Status
Gia_Divine
Ubg98.Github.io Unblocked
Johnnie Walker Double Black Costco
Dark Entreaty Ffxiv
Drift Hunters - Play Unblocked Game Online
Ficoforum
Watertown Ford Quick Lane
Co10 Unr
How to Use Craigslist (with Pictures) - wikiHow
Darktide Terrifying Barrage
Evil Dead Rise - Everything You Need To Know
Aid Office On 59Th Ashland
Homewatch Caregivers Salary
Craigslist Central Il
Wbli Playlist
Justin Mckenzie Phillip Bryant
Www Violationinfo Com Login New Orleans
Ducky Mcshweeney's Reviews
Cruise Ships Archives
Iban's staff
Property Skipper Bermuda
Fetus Munchers 1 & 2
M Life Insider
Directions To The Closest Auto Parts Store
Juiced Banned Ad
What is a lifetime maximum benefit? | healthinsurance.org
Maplestar Kemono
Marcel Boom X
Smoke From Street Outlaws Net Worth
Worlds Hardest Game Tyrone
Latest Posts
Article information

Author: Delena Feil

Last Updated:

Views: 5995

Rating: 4.4 / 5 (65 voted)

Reviews: 80% of readers found this page helpful

Author information

Name: Delena Feil

Birthday: 1998-08-29

Address: 747 Lubowitz Run, Sidmouth, HI 90646-5543

Phone: +99513241752844

Job: Design Supervisor

Hobby: Digital arts, Lacemaking, Air sports, Running, Scouting, Shooting, Puzzles

Introduction: My name is Delena Feil, I am a clean, splendid, calm, fancy, jolly, bright, faithful person who loves writing and wants to share my knowledge and understanding with you.