What is the prop.table() function in R?
Overview
The prop.table() function in R creates table entries as fractions of a marginal table.
The prop.table() function shows the individual value as a fraction of the whole. These fractions (the values in the table) sum up to 1.
Syntax
The prop.table() function takes the syntax shown in the code widget below.
prop.table(x, margin=NULL)
Syntax for the prop.table() function in R
Parameter value
The prop.table() function takes the following parameter values:
x: This is the R object that will serve as the table. This is a required parameter.margin: This is an index or vector of indices for which the margin is generated. This is an optional parameter.
Return value
The prop.table() function returns a table expressed relative to the margin.
Example
# creating a matrix objectmy_matrix <- matrix (c( 1, 3, 1,7, 3, 5),ncol = 3,byrow = TRUE)# implementing the prop.table() functionprop.table(my_matrix)
Explanation
- Line 2–7: We create a matrix object
my_matrixusing thematrix()function. - Line 10: Using the
prop.table()function, we return a table showing each value as a proportion of the whole value in the table. When these proportions are added, they equal1.