汇总每天的data.frame

时间:2016-01-28 21:01:56

标签: r aggregate

我有一个关于汽车销售的数据框架dat(数据框中为Buy=0)并在二手车销售商处购买(Buy=1)。

  Date       Buy   Price
29-06-2015    1    5000
29-06-2015    0    8000
29-06-2015    1    10000
30-06-2015    0    3500
30-06-2015    0    12000 
...          ...  ...

我需要的是一个新的,汇总的data.frame,它给我每天的购买数量和销售数量,以及当天所有购买和销售的总价格:

  Date      Buys   Sells   Price_Buys  Price_Sells
29-06-2015    2    1         15000        8000
30-06-2015    0    2           0          15500
...          ...  ...

我尝试使用aggregate(dat$Buy, by=list(Date=dat$Date, FUN=sum))。但是,我仍然在努力如何聚合销售。

4 个答案:

答案 0 :(得分:6)

这可以在dplyr中非常干净地完成,使用group_by按日期分组,然后使用summarize进行汇总:

library(dplyr)
(out <- dat %>%
  group_by(Date) %>%
  summarize(Buys=sum(Buy == 1), Sells=sum(Buy == 0),
            Price_Buys=sum(Price[Buy == 1]), Price_Sells=sum(Price[Buy == 0])))
#         Date  Buys Sells Price_Buys Price_Sells
#       (fctr) (int) (int)      (int)       (int)
# 1 29-06-2015     2     1      15000        8000
# 2 30-06-2015     0     2          0       15500

您现在可以像处理普通数据框一样操纵此对象,例如有类似的东西:

out$newvar <- with(out, Sells*Price_Sells - Buys*Price_Buys)
out
# Source: local data frame [2 x 6]
#         Date  Buys Sells Price_Buys Price_Sells newvar
#       (fctr) (int) (int)      (int)       (int)  (int)
# 1 29-06-2015     2     1      15000        8000 -22000
# 2 30-06-2015     0     2          0       15500  31000

答案 1 :(得分:4)

使用data.table V 1.9.6+,您现在可以为fun参数提供一系列函数,因此我们可以使用dcast)轻松解决此问题,而无需指定任何参数手工条件

library(data.table) # V1.9.6+
dcast(setDT(dat), Date ~ Buy , value.var = "Price", fun = list(length, sum))
#          Date Price_length_0 Price_length_1 Price_sum_0 Price_sum_1
# 1: 29-06-2015              1              2        8000       15000
# 2: 30-06-2015              2              0       15500           0

或者,如果我们想尝试dplyr,那么解决此问题的强大方法(再次,无需指定任何条件)可能

library(dplyr)
df %>%
  group_by(Date, Buy) %>%
  summarise_each(funs(sum, length), Price)

# Source: local data frame [3 x 4]
# Groups: Date [?]
# 
#         Date   Buy   sum length
#       (fctr) (int) (int)  (int)
# 1 29-06-2015     0  8000      1
# 2 29-06-2015     1 15000      2
# 3 30-06-2015     0 15500      2

答案 2 :(得分:3)

您可以使用库dplyr执行此操作:

df %>% group_by(Date) %>% summarise(buys = sum(Buy == 1), sells = sum(Buy == 0), Price_Buys = sum(Price[Buy == 1]), Price_Sells = sum(Price[Buy == 0]))
Source: local data frame [2 x 5]

        Date  buys sells Price_Buys Price_Sells
      (fctr) (int) (int)      (int)       (int)
1 29-06-2015     2     1      15000        8000
2 30-06-2015     0     2          0       15500

答案 3 :(得分:3)

我会自己使用其中一个dpylr解决方案,但我认为它仍然值得注意,它也可以使用aggregate()来完成,因为这是你开始的方式:

aggregate(cbind(Buys = Buy, Sells = !Buy,
                Price_Buys = Price * Buy, Price_Sells = Price * !Buy) ~ Date,
          data = dat, sum)
##         Date Buys Sells Price_Buys Price_Sells
## 1 29-06-2015    2     1      15000        8000
## 2 30-06-2015    0     2          0       15500

这里的想法是将销售额定为!Buy。这会将Buy转换为逻辑(0 =&gt; TRUE,1 =&gt; FALSE),然后将NOT运算符(!)应用于它。这样,0转换为1,1转换为0.计算价格时可以使用相同的技巧。

此解决方案与其他解决方案的比较也应该向您显示,dplyr生成更易读的代码。

相关问题