汇总R中的数据 [英] Summarize data in R

查看:95
本文介绍了汇总R中的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据集,其中包含按网点每周销售的各种产品。数据如下所示:

I have a dataset which contains weekly sale of various products by outlet. Here is how the data looks like:

Store ID    Week ID Item Code   Sales in $
253422  191 41130   2.95
272568  188 41130   2.95
272568  188 41160   2.95
272568  189 41130   2.95
272568  189 41160   2.95
272568  190 41160   2.95
217460  188 41110   2.95
217460  188 41130   5.9
217460  188 41160   5.9
217460  189 41110   11.8
217460  189 41130   8.85
217460  189 41160   11.8
217460  191 41130   5.95
217460  191 41160   8.93

这是一个非常大的数据集,我想生成一个摘要输出ITEM明智的总销售额和该商品所在的商店数量。我尝试了以下操作,但这不起作用,因为我得到的存储计数由于数据集中重复几周而重复:

This is a very large dataset and I would like to generate a summary output which gives me the ITEM wise total sales and the number of stores in which the item is present. I tried the following, but that doesn't work because I get a store count which is repeated due to the repetition of weeks in the dataset:

dataset %>% group_by(Store ID) %>% summarize(count(Item Code))

我们非常感谢您的帮助。
谢谢

Any help is highly appreciated. Thanks

推荐答案

您可以使用汇总

## Recreate your data
df = read.table(text="'Store ID'    'Week ID' 'Item Code'   'Sales in Dollars'
253422  191 41130   2.95
272568  188 41130   2.95
272568  188 41160   2.95
272568  189 41130   2.95
272568  189 41160   2.95
272568  190 41160   2.95
217460  188 41110   2.95
217460  188 41130   5.9
217460  188 41160   5.9
217460  189 41110   11.8
217460  189 41130   8.85
217460  189 41160   11.8
217460  191 41130   5.95
217460  191 41160   8.93",
header=TRUE)

aggregate(df$Sales.in.Dollars, list(df$Item.Code), sum)




  Group.1     x
1   41110 14.75
2   41130 29.55
3   41160 35.48




StoreItems = unique(df[,c(1,3)])
aggregate(StoreItems$Store.ID, list(StoreItems$Item.Code), length)




  Group.1 x
1   41110 1
2   41130 3
3   41160 2


这篇关于汇总R中的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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