# Calculate Percentage in R (2 Examples)

In this tutorial you’ll learn how to **create a table of probabilities** in the R programming language.

Table of contents:

So let’s get started.

## Introduction of Exemplifying Data

The following data will be used as basement for this R programming language tutorial.

set.seed(269837) # Example data x <- sample(letters[1:10], 50, replace = TRUE) head(x) # Head of example data # [1] "g" "a" "d" "g" "e" "a" |

set.seed(269837) # Example data x <- sample(letters[1:10], 50, replace = TRUE) head(x) # Head of example data # [1] "g" "a" "d" "g" "e" "a"

The previous output of the RStudio console shows that our example data consists of a random sequence of alphabetical letters.

## Example 1: Create Table with Counts of Each Value in Vector

In Example 1, I’ll show how to create a count table of our example vector. This will be used as basement for the second example, in which I will calculate the percentages for the occurrences of each value in our vector.

However, let’s first have a look at the following R syntax and its output:

x_cout <- table(x) # Create counts for each value x_cout # Print table of counts # x # a b c d e f g h i j # 10 4 6 5 3 6 4 2 4 6 |

x_cout <- table(x) # Create counts for each value x_cout # Print table of counts # x # a b c d e f g h i j # 10 4 6 5 3 6 4 2 4 6

As you can see, we have used the table function to create a frequency table that shows the counts of each value in our data.

Let’s convert these counts into percentages!

## Example 2: Create Table with Percent of Each Value in Vector

The following R programming syntax explains how to calculate percentage points from a vector of counts.

For this, we can use the frequency table that we have created in the previous example and the length function:

x_percent <- x_cout / length(x) # Create percentage table x_percent # Print table of percentages # x # a b c d e f g h i j # 0.20 0.08 0.12 0.10 0.06 0.12 0.08 0.04 0.08 0.12 |

x_percent <- x_cout / length(x) # Create percentage table x_percent # Print table of percentages # x # a b c d e f g h i j # 0.20 0.08 0.12 0.10 0.06 0.12 0.08 0.04 0.08 0.12

The previous output of the RStudio console shows our table of probabilities / percentages. For instance, the letter has a share of 20 %, the letter b has a share of 8 %, and so on…

## Video, Further Resources & Summary

Do you need more explanations on the R programming syntax of the present post? Then I recommend having a look at the following video of my YouTube channel. I show the R syntax of this article in the video:

**Please accept YouTube cookies to play this video.** By accepting you will be accessing content from YouTube, a service provided by an external third party.

If you accept this notice, your choice will be saved and the page will refresh.

Furthermore, you might have a look at the related tutorials on this website. I have released numerous tutorials about percentages already:

- Extend Contingency Table with Proportions & Percentages
- Cumulative Frequency & Probability Table
- Format Number as Percentage in R
- Introduction to R

Summary: In this tutorial, I have shown how to **calculate percentage shares of each value in a vector** in R. In case you have additional comments and/or questions, please tell me about it in the comments below. Besides that, don’t forget to subscribe to my email newsletter in order to get updates on the newest articles.

### Statistics Globe Newsletter