由两个变量求和 [英] Sum by two variables
本文介绍了由两个变量求和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个数据框:
Date area sales
1 201204 shanghai 23
2 201204 beijing 25
3 201204 beijing 16
4 201205 shanghai 55
5 201205 beijing 17
6 201205 shanghai 16
我要输出的是一个表格如下:
What I want to output is a table as follows:
Date shanghai beijing
201204 23 41
201205 71 17
我将如何在 R 中执行此操作?
How would I do this in R?
推荐答案
在基础 R(对于 sum
)中有 xtabs
:
In base R (for sum
) there's xtabs
:
> xtabs(sales ~ Date + area, mydf)
area
Date beijing shanghai
201204 41 23
201205 17 71
要将其作为 data.frame
获取,请将其包装在 as.data.frame.matrix
中.
To get it as a data.frame
, wrap it in as.data.frame.matrix
.
要使用最近流行的方法更新它,您还可以使用dplyr"(用于聚合)和tidyr"(用于重塑)的组合,如下所示:
To update this with the approach that is making the rounds these days, you can also use a combination of "dplyr" (for aggregation) and "tidyr" (for reshaping), like this:
library(tidyr)
library(dplyr)
mydf %>%
group_by(Date, area) %>%
summarise(sales = sum(sales)) %>%
spread(area, sales)
# Source: local data frame [2 x 3]
#
# Date beijing shanghai
# 1 201204 41 23
# 2 201205 17 71
这篇关于由两个变量求和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文