# Extend Contingency Table with Proportions & Percentages in R (4 Examples)

In this article, Iâ€™ll illustrate how to **create a table containing counts, proportions, and percentages** in R.

Table of contents:

Letâ€™s get startedâ€¦

## Creation of Example Data

Have a look at the following example data:

set.seed(15630497) # Create example values values <- round(rnorm(100, 100, 2)) values # Print vector to RStudio console # [1] 100 101 99 99 95 99 99 102 99 101 97 100 100 98 96 101 103 101 100 ...

Have a look at the previous output of the RStudio console. It shows that our exemplifying data is a vector consisting of 100 numeric values.

The following examples show how to illustrate these values in contingency, proportion, and percentage tables in R.

Letâ€™s do this!

## Example 1: Creating Contingency Table Using table() Function

In this example, Iâ€™ll illustrate how to create a frequency table using the table() function in R.

Have a look at the following R syntax and its output:

contingency_table <- table(values) # Create contingency table contingency_table # Print contingency table # values # 95 96 97 98 99 100 101 102 103 104 105 # 2 4 10 9 16 19 16 12 8 2 2

The upper line of the table shows the values in our example data. The lower line of the table shows the counts of each of these values.

For instance, the value 95 appears twice in our example data.

## Example 2: Creating Proportion Table Using prop.table() Function

In this section, Iâ€™ll illustrate how to use the prob.table function to convert our contingency table (see Example 1) to a proportion table:

proportion_table <- prop.table(contingency_table) # Create proportion table proportion_table # Print proportion table # values # 95 96 97 98 99 100 101 102 103 104 105 # 0.02 0.04 0.10 0.09 0.16 0.19 0.16 0.12 0.08 0.02 0.02

The values that are shown in the previous table represent the proportions of each value in our example vector.

## Example 3: Creating Percentage Table by Multiplying Proportions with 100

This section shows how to convert the proportions (see Example 2) to percentages. For this, we simply have to multiply our proportion table by 100.

percentage_table <- proportion_table * 100 # Create percentage table percentage_table # Print percentage table # values # 95 96 97 98 99 100 101 102 103 104 105 # 2 4 10 9 16 19 16 12 8 2 2

## Example 4: Creating Data Matrix Containing Contingencies, Proportions & Percentages

Example 4 shows how to combine the different metrics that we have calculated before (i.e. frequency counts, proportions, and percentages) in a single data matrix. For this, we can use the rbind function:

table_all <- rbind(contingency_table, # Create matrix with all values proportion_table, percentage_table) table_all # Print matrix with all values # 95 96 97 98 99 100 101 102 103 104 105 # contingency_table 2.00 4.00 10.0 9.00 16.00 19.00 16.00 12.00 8.00 2.00 2.00 # proportion_table 0.02 0.04 0.1 0.09 0.16 0.19 0.16 0.12 0.08 0.02 0.02 # percentage_table 2.00 4.00 10.0 9.00 16.00 19.00 16.00 12.00 8.00 2.00 2.00

We have extended our contingency table by proportions and percentages. Looks good!

## Video & Further Resources

In case you need further explanations on the R programming code of this page, you may watch the following video of my YouTube channel. In the video, I explain the R syntax of this page:

In addition to the video, you could read the related tutorials on this website. You can find some tutorials below.

- Proportions with dplyr Package in R
- Count Unique Values in R
- Get Frequency of Elements with Certain Value in Vector
- R Programming Tutorials

In this tutorial you learned how to **construct a contingency, proportion, and percentage matrix** in the R programming language. If you have additional questions, please let me know in the comments section.