查找跨类别的比例,使用ddply按第二个类别分组 [英] Find proportion across categories, grouped by a second category using ddply

查看:63
本文介绍了查找跨类别的比例,使用ddply按第二个类别分组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想找到一个给定类别中数值的百分比分布,但按第二个类别分组.例如,假设我有一个包含regionline_of_businesssales的数据框,并且我想找到按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的内置aggregatemerge函数来做到这一点,但是我很好奇是否有一种更短的方法可以使用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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆