How to Make a Prop Table in R (Example Code)

This tutorial demonstrates how to apply the prop.table function in the R programming language.

Creation of Example Data

data(iris)                         # Example data
iris_sub <- iris[ , c(1, 5)]
head(iris)
#   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
# 1          5.1         3.5          1.4         0.2  setosa
# 2          4.9         3.0          1.4         0.2  setosa
# 3          4.7         3.2          1.3         0.2  setosa
# 4          4.6         3.1          1.5         0.2  setosa
# 5          5.0         3.6          1.4         0.2  setosa
# 6          5.4         3.9          1.7         0.4  setosa
iris_tab <- table(iris_sub)        # Example table
iris_tab
#             Species
# Sepal.Length setosa versicolor virginica
#          4.3      1          0         0
#          4.4      3          0         0
#          4.5      1          0         0
#          4.6      4          0         0
#          4.7      2          0         0
#          4.8      5          0         0
#          4.9      4          1         1
#          5        8          2         0
#          5.1      8          1         0
#          5.2      3          1         0
#          5.3      1          0         0
#          5.4      5          1         0
#          5.5      2          5         0
#          5.6      0          5         1
#          5.7      2          5         1
#          5.8      1          3         3
#          5.9      0          2         1
#          6        0          4         2
#          6.1      0          4         2
#          6.2      0          2         2
#          6.3      0          3         6
#          6.4      0          2         5
#          6.5      0          1         4
#          6.6      0          2         0
#          6.7      0          3         5
#          6.8      0          1         2
#          6.9      0          1         3
#          7        0          1         0
#          7.1      0          0         1
#          7.2      0          0         3
#          7.3      0          0         1
#          7.4      0          0         1
#          7.6      0          0         1
#          7.7      0          0         4
#          7.9      0          0         1

Example: Make a Prop Table Using prop.table() Function

prop.table(iris_tab)               # Create proportions table
#             Species
# Sepal.Length      setosa  versicolor   virginica
#          4.3 0.006666667 0.000000000 0.000000000
#          4.4 0.020000000 0.000000000 0.000000000
#          4.5 0.006666667 0.000000000 0.000000000
#          4.6 0.026666667 0.000000000 0.000000000
#          4.7 0.013333333 0.000000000 0.000000000
#          4.8 0.033333333 0.000000000 0.000000000
#          4.9 0.026666667 0.006666667 0.006666667
#          5   0.053333333 0.013333333 0.000000000
#          5.1 0.053333333 0.006666667 0.000000000
#          5.2 0.020000000 0.006666667 0.000000000
#          5.3 0.006666667 0.000000000 0.000000000
#          5.4 0.033333333 0.006666667 0.000000000
#          5.5 0.013333333 0.033333333 0.000000000
#          5.6 0.000000000 0.033333333 0.006666667
#          5.7 0.013333333 0.033333333 0.006666667
#          5.8 0.006666667 0.020000000 0.020000000
#          5.9 0.000000000 0.013333333 0.006666667
#          6   0.000000000 0.026666667 0.013333333
#          6.1 0.000000000 0.026666667 0.013333333
#          6.2 0.000000000 0.013333333 0.013333333
#          6.3 0.000000000 0.020000000 0.040000000
#          6.4 0.000000000 0.013333333 0.033333333
#          6.5 0.000000000 0.006666667 0.026666667
#          6.6 0.000000000 0.013333333 0.000000000
#          6.7 0.000000000 0.020000000 0.033333333
#          6.8 0.000000000 0.006666667 0.013333333
#          6.9 0.000000000 0.006666667 0.020000000
#          7   0.000000000 0.006666667 0.000000000
#          7.1 0.000000000 0.000000000 0.006666667
#          7.2 0.000000000 0.000000000 0.020000000
#          7.3 0.000000000 0.000000000 0.006666667
#          7.4 0.000000000 0.000000000 0.006666667
#          7.6 0.000000000 0.000000000 0.006666667
#          7.7 0.000000000 0.000000000 0.026666667
#          7.9 0.000000000 0.000000000 0.006666667

Leave a Reply

Your email address will not be published. Required fields are marked *

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