在 R 中,如何计算给定不同变量的因子百分比? [英] In R, how do I compute factors' percentage given on different variable?
问题描述
我正在尝试计算变量中因子的百分比,并希望该百分比以其他变量为条件.
I am trying to compute percentage of factors in a variable, and want to make that percentage conditional on other variable.
例如,我有这样的数据.
For example, I have data like this.
State Ideology
CO Liberal
CO Liberal
CO Liberal
CO Conservative
CO Conservative
CO Independent
DC Independent
DC Conservative
DC Liberal
我试图找出每个州的每个自由党、保守党和独立党的百分比.
I am trying to find the percentage of each Liberal, Conservative, and Independent on each state.
我尝试使用 ddply 之类的
I tried to use ddply like
liberal_per<-ddply(data,.(State), summarize,total=table(Ideology)[1]/sum(Ideology))
但它不起作用.我应该如何尝试找到 State 上给出的每个因素的百分比?
But it doesn't work. How should I try to find percentage of each factor given on State?
谢谢!
推荐答案
因为 State
在数据框中排在第一位,table
将使用它作为行 ID.因此,您可以将 table
的结果除以行总和以获得比率,或缩放到百分比.
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.
表格:
> table(x)
Ideology
State Conservative Independent Liberal
CO 2 1 3
DC 1 1 1
使用 prop.table
进行缩放,获取每个状态的值:
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
这相当于table(x)/rowSums(table(x))
如果需要,您可以乘以 100 以获得百分比值.
You can multiply by 100 to get percent values if needed.
这篇关于在 R 中,如何计算给定不同变量的因子百分比?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!