19

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
  • 1
    What do you get for 1÷4, 3÷4, 2÷6, and 4÷6? The 1 in prop.table represents row-wise calculations. Try it with 2 instead for comparison. Aug 11, 2017 at 11:20
  • If you don't provide a margin, it calculates m / sum(m). With margins, this is done row- or column-wise.
    – Roland
    Aug 11, 2017 at 11:24
  • 1
    See also ?margin.table. Aug 11, 2017 at 11:24

3 Answers 3

34

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)
9

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
>

0

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
  • when prop.table(m, n) where n is NOT specified (absent), then each element is calculated to a percentage of TOTAL SUM of the matrix elements.
    – r poon
    Apr 27, 2018 at 10:35

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.