2019-06-28

Proportion tables in R

Problem

We want to create proportion tables for one or multiple variables.

Solution

  • One variable
  • tabla <- table(mtcars$am)
    prop.table(tabla)
    
          0       1 
    0.59375 0.40625
    
  • Two variables
  • tabla <- table(mtcars[, c("am", "gear")])
    prop.table(tabla)
    
       gear
    am        3       4       5
      0 0.46875 0.12500 0.00000
      1 0.00000 0.25000 0.15625
    
    The prop.table function has two arguments:

    • x, table created with the function table
    • margin, with three possible values:
    •   Null - x/sum(x) default like in the previous example.
        1 - proportion calculated by rows.
        2 - proportion calculated by columns.

    # By row
    prop.table(tabla, 1)
    
       gear
    am          3         4         5
      0 0.7894737 0.2105263 0.0000000
      1 0.0000000 0.6153846 0.3846154
    
    # By column
    prop.table(tabla, 2)
    
       gear
    am          3         4         5
      0 1.0000000 0.3333333 0.0000000
      1 0.0000000 0.6666667 1.0000000
    
  • Three variables
  • tabla <- table(mtcars[, c("am", "gear", "cyl")])
    prop.table(tabla)
    
    , , cyl = 4
    
       gear
    am        3       4       5
      0 0.03125 0.06250 0.00000
      1 0.00000 0.18750 0.06250
    
    , , cyl = 6
    
       gear
    am        3       4       5
      0 0.06250 0.06250 0.00000
      1 0.00000 0.06250 0.03125
    
    , , cyl = 8
    
       gear
    am        3       4       5
      0 0.37500 0.00000 0.00000
      1 0.00000 0.00000 0.06250
    
  • Flat Contingency Table
  • In the previous example, a better approach would be to create a flat contingency table..

    tabla <- ftable(mtcars[, c("am", "gear", "cyl")])
    prop.table(tabla)
    
            cyl       4       6       8
    am gear                            
    0  3        0.03125 0.06250 0.37500
       4        0.06250 0.06250 0.00000
       5        0.00000 0.00000 0.00000
    1  3        0.00000 0.00000 0.00000
       4        0.18750 0.06250 0.00000
       5        0.06250 0.03125 0.06250
    
  • Percentage table
  • We can use the function round.

    round(prop.table(tabla)*100, 2)
    
             cyl     4     6     8
    am gear                      
    0  3         3.12  6.25 37.50
       4         6.25  6.25  0.00
       5         0.00  0.00  0.00
    1  3         0.00  0.00  0.00
       4        18.75  6.25  0.00
       5         6.25  3.12  6.25
    
    round(prop.table(tabla, 1)*100, 2) # By row, am y gear.
    
            cyl     4     6     8
    am gear                      
    0  3         6.67 13.33 80.00
       4        50.00 50.00  0.00
       5          NaN   NaN   NaN
    1  3          NaN   NaN   NaN
       4        75.00 25.00  0.00
       5        40.00 20.00 40.00
    
    round(prop.table(tabla, 2)*100, 2) # By column, cyl
    
            cyl     4     6     8
    am gear                      
    0  3         9.09 28.57 85.71
       4        18.18 28.57  0.00
       5         0.00  0.00  0.00
    1  3         0.00  0.00  0.00
       4        54.55 28.57  0.00
       5        18.18 14.29 14.29
    

References

No hay comentarios:

Publicar un comentario

Nube de datos