Home > OS >  percentages of a single column of a table in r
percentages of a single column of a table in r

Time:10-09

I have a table of this style:

     a         b   c     d
1  225.4       45  1920  1      
2  812.3       101 1930  1      
3  623.7       23  1965  2      
4  551.7       32  1975  3      
5 1374.1       91  1975  3      
6  931.0       64  1912  3  

How can I get a proportion table of the column d that gets me something like this: 1 33.3 2 16.7 3 50.0

With table(df$d) I get

    1    2    3 
    2    1    3 

But with prop.table command I don't get the same results with proportions.

CodePudding user response:

You need both table and prop.table.

prop.table(table(df$d))

#        1         2         3 
#0.3333333 0.1666667 0.5000000 

CodePudding user response:

We may use proportions from base R

proportions(table(df$d))
  •  Tags:  
  • r
  • Related