In R, how do I compute factors' percentage given on different variable? [duplicate]

蓝咒 提交于 2019-12-03 08:34:40

Because State comes first in the data frame, table will use that as the row ID. Thus, you can divide the results of table by the row sums to get ratios, or scale to percentage.

The table:

> table(x)
     Ideology
State Conservative Independent Liberal
   CO            2           1       3
   DC            1           1       1

Using prop.table to do the scaling, to get values per-state:

> prop.table(table(x), 1)
     Ideology
State Conservative Independent   Liberal
   CO    0.3333333   0.1666667 0.5000000
   DC    0.3333333   0.3333333 0.3333333

This is equivalent to table(x)/rowSums(table(x))

You can multiply by 100 to get percent values if needed.

You could modify your ddply code to:

 ddply(data,.(State), 
    function(x) with(x,
      data.frame(100*round(table(Ideology)/length(Ideology),2))))

 #    State     Ideology Freq
 #1    CO Conservative   33
 #2    CO  Independent   17
 #3    CO      Liberal   50
 #4    DC Conservative   33
 #5    DC  Independent   33
 #6    DC      Liberal   33
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!