# Calculate Percentage in R (2 Examples)

In this tutorial youâ€™ll learn how to create a table of probabilities in the R programming language.

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)
# [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```

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```

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:

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

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.

Subscribe to the Statistics Globe Newsletter