查找跨类别的比例,使用ddply按第二个类别分组 [英] Find proportion across categories, grouped by a second category using ddply
问题描述
我想找到一个给定类别中数值的百分比分布,但按第二个类别分组.例如,假设我有一个包含region
,line_of_business
和sales
的数据框,并且我想找到按line_of_business
分组的sales
百分比,并按region
分组.
I want to find the percentage distribution of a numerical value across a given category, but grouped by a second category. For example, suppose I have a data frame with region
, line_of_business
, and sales
, and I want to find the percentage of sales
by line_of_business
, grouped by region
.
我可以使用R的内置aggregate
和merge
函数来做到这一点,但是我很好奇是否有一种更短的方法可以使用plyr
的'ddply
函数来避免显式调用merge
.
I could do this with R's built-in aggregate
and merge
functions but I was curious if there was an shorter way to do this with plyr
's 'ddply
function that avoids an explicit call to merge
.
推荐答案
如何创建交叉表并采用比例?
How about creating a crosstab and taking proportions?
total_sales <- xtabs(sales~region+line_of_business, data=df)
prop.table(total_sales, 1)
这篇关于查找跨类别的比例,使用ddply按第二个类别分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!