Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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"?

like image 325
The Pointer Avatar asked Aug 11 '17 11:08

The Pointer


2 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)
like image 137
trosendal Avatar answered Sep 20 '22 15:09

trosendal


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
>

like image 41
Mamun Or Rashid Avatar answered Sep 18 '22 15:09

Mamun Or Rashid