Create Table of Percentages in R (Example Code)

In this post you’ll learn how to calculate percentage shares of each value in a vector in the R programming language.

Creation of Example Data

set.seed(9843576)                             # Creating example vector in R
my_values <- sample(1:5, 200, replace = TRUE)
head(my_values)                               # Show first values of example vector in RStudio console
# [1] 3 1 3 1 2 3

Example: Calculate Percentages in R Using table() & length() Functions

table(my_values) * 100 / length(my_values)    # Calculate percentages
# my_values
#    1    2    3    4    5 
# 14.0 19.5 23.0 21.5 22.0

Leave a Reply

Your email address will not be published.

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed

Menu
Top