按分类变量求和
我有一个按卷计算的漫画书单位销售数据集(例如火影忍者 v10),我需要将其减少为按系列销售(因此所有火影忍者卷单位销售将被添加到一个观察中)。我有一个变量“系列”,用于标识每个观察的系列。 Stata 中的等效代码是:
by series, sort:replace unitssales=sum(unitssales);
by series, sort:keep if _n==_N
但我正在尝试弄清楚如何在 R 中执行此操作。任何帮助将不胜感激!提前致谢!
I have a data set of comic book unit sales by volume (ex. Naruto v10) that I need to reduce to sales by series (so all Naruto volume unit sales would be added together into a single observation). I have a variable "series" that identifies the series of each observation. The equivalent code in Stata would be:
by series, sort:replace unitssales=sum(unitssales);
by series, sort:keep if _n==_N
But I'm trying to figure out how to do this in R. Any help would be much appreciated! Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在不知道您的数据是什么格式的情况下,我只能建议您查看
tapply
函数。来自帮助:Without knowing what format your data is in, I can only suggest you look at the
tapply
function. From the help: