通过连续运行变量对数据进行分组

时间:2018-07-05 12:35:51

标签: r dataframe group-by dplyr

我想按type对数据进行分组,这样我必须将Qtyamount进行分组时,我有1个已卖出,1个又卖出了……。对于Date,代码可以保留第二个Date

中的第一个
1    Type                    Date        Qty       Price        Amount             
2    Sold 2018-06-28 20:47:48 UTC     0.0078   667514.96   5206.616688             
3    Sold 2018-06-28 20:47:48 UTC     0.0022   667514.96   1468.532912             
4  Bought 2018-06-28 20:48:17 UTC       0.01 668624.1841   6686.241841             
5    Sold 2018-06-28 20:48:33 UTC      0.005   668424.97    3342.12485             
6    Sold 2018-06-28 20:48:35 UTC      0.005   668435.82     3342.1791             
7  Bought 2018-06-28 20:48:50 UTC       0.01   667898.67     6678.9867             
8    Sold 2018-06-28 20:48:58 UTC       0.01   667881.57     6678.8157             
9  Bought 2018-06-28 20:49:54 UTC       0.01   668941.24     6689.4124             
10 Bought 2018-06-28 20:57:58 UTC      0.001   668442.05     668.44205             
11 Bought 2018-06-28 20:58:31 UTC      0.009   668602.02    6017.41818             
12   Sold 2018-06-28 20:58:35 UTC       0.01   668293.98     6682.9398             
13 Bought 2018-06-28 20:59:04 UTC       0.01   668626.02     6686.2602             
14   Sold 2018-06-28 20:59:43 UTC       0.01   668892.83     6688.9283             
15 Bought 2018-06-28 20:59:54 UTC       0.01   669230.64     6692.3064             
16   Sold 2018-06-28 21:02:40 UTC       0.01   668746.96     6687.4696             
17 Bought 2018-06-28 21:02:49 UTC       0.01   668019.58     6680.1958             
18   Sold 2018-06-28 21:03:43 UTC       0.01   667884.01     6678.8401             
19 Bought 2018-06-28 21:08:13 UTC      0.004 668007.2834   2672.029133

例如,首先要对第2行和第3行进行分组,然后离开第4行,对第5行和第6行进行分组。并且9、10和11也必须进行分组。

这是一个例子

1         Type                 Date           Qty         Amount             
2&3       Sold  2018-06-28 20:47:48      sum(2&3)       sum(2&3)                         
4       Bought  2018-06-28 20:48:17          0.01    6686.241841             
5&6       Sold  2018-06-28 20:48:33      sum(5&6)       sum(5&6)    
7       Bought  2018-06-28 20:48:50          0.01      6678.9867            
8         Sold  2018-06-28 20:48:58          0.01      6678.8157             
9$10&11 Bought  2018-06-28 20:49:54  sum(9&10&11)   sum(9&10&11)             
12        Sold  2018-06-28 20:58:35          0.01      6682.9398             
...

我已经尝试过dplyr,但如果有人有主意,还是没有成功,谢谢。

0 个答案:

没有答案