How does the `prop.table()` function work in r?

I've just started learning r and have had trouble finding an (understandable) explanation of what the prop.table() function does. I found the following explanation and example:

prop.table: Express Table Entries as Fraction of Marginal Table

Examples

m <- matrix(1:4, 2) m prop.table(m, 1) 

But, as a beginner, I do not understand what this explanation means. I've also attempted to discern its functionality from the result of the above example, but I haven't been able to make sense of it.

With reference to the example above, what does the prop.table() function do? Furthermore, what is a "marginal table"?

3

3 Answers

The values in each cell divided by the sum of the 4 cells:

prop.table(m) 

The value of each cell divided by the sum of the row cells:

prop.table(m, 1) 

The value of each cell divided by the sum of the column cells:

prop.table(m, 2) 

I think this can help

include all those things like prop.table(m), prop.table(m, 1), prop.table(m, 2)

m <- matrix(1:4, 2) > m [,1] [,2] [1,] 1 3 [2,] 2 4 > prop.table(m) #sum=1+2+3+4=10, 1/10=0.1, 2/10=0.2, 3/10=0.3,4/10=0.4 [,1] [,2] [1,] 0.1 0.3 [2,] 0.2 0.4 > prop.table(m,1) [,1] [,2] [1,] 0.2500000 0.7500000 #row1: sum=1+3=4, m(0,0)=1/4=0.25, m(0,1)=3/4=0.75 [2,] 0.3333333 0.6666667 #row2: sum=2+4=6, m(1,0)=2/6=0.33, m(1,1)=4/6=0.66 > prop.table(m,2) [,1] [,2] [1,] 0.3333333 0.4285714 #col1: sum=1+2=3, m(0,0)=1/3=0.33, m(1,0)=2/3=0.4285 [2,] 0.6666667 0.5714286 #col2: sum=3+4=7, m(0,1)=3/7=0.66, m(1,1)=4/7=0.57 > 

when m is the 2D matrix: (m,1) refers to a fraction of row marginal table (sum over each row), (m,2) refers to a fraction of column marginal table (sum over each column). In short, just a "% of total sum of row of column", if you dont want to care about the term marginal.

Example:

m with extra row and column margin

 [,1] [,2] *** [1,] 1 4 5 [2,] 2 5 7 [3,] 3 6 9 *** 6 15 > prop.table(m,1) ` [,1] [,2] [1,] 0.2000000 0.8000000 [2,] 0.2857143 0.7142857 [3,] 0.3333333 0.6666667 > prop.table(m,2) [,1] [,2] [1,] 0.1666667 0.2666667 [2,] 0.3333333 0.3333333 [3,] 0.5000000 0.4000000 
1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like