gpt4 book ai didi

r - R data.table 中的分组计数聚合

转载 作者:行者123 更新时间:2023-12-04 11:01:03 25 4
gpt4 key购买 nike

包含日期、买入值和卖出值的表格。我想计算每天的买卖次数以及买卖的总数。我发现在 data.table 中这样做有点棘手。

   date   buy sell      
2011-01-01 1 0
2011-01-02 0 0
2011-01-03 0 2
2011-01-04 3 0
2011-01-05 0 0
2011-01-06 0 0
2011-01-01 0 0
2011-01-02 0 1
2011-01-03 4 0
2011-01-04 0 0
2011-01-05 0 0
2011-01-06 0 0
2011-01-01 0 0
2011-01-02 0 8
2011-01-03 2 0
2011-01-04 0 0
2011-01-05 0 0
2011-01-06 0 5

上面的 data.table 可以使用以下代码创建:
 DT = data.table(
date=rep(as.Date('2011-01-01')+0:5,3) ,
buy=c(1,0,0,3,0,0,0,0,4,0,0,0,0,0,2,0,0,0),
sell=c(0,0,2,0,0,0,0,1,0,0,0,0,0,8,0,0,0,5));

结果我想要的是:
   date   total_buys   total_sells
2011-01-01 1 0
2011-01-02 0 2
and so on

此外,我还想知道买卖的总数:
 total_buys   total_sells
4 4

我试过了 :
 length(DT[sell > 0 | buy > 0])
> 3

这是一个奇怪的答案(想知道为什么)

最佳答案

## by date
DT[, list(total_buys = sum(buy > 0), total_sells = sum(sell > 0)), by = date]
## date total_buys total_sells
## 1: 2011-01-01 1 0
## 2: 2011-01-02 0 2
## 3: 2011-01-03 2 1
## 4: 2011-01-04 1 0
## 5: 2011-01-05 0 0
## 6: 2011-01-06 0 1

DT[, list(total_buys = sum(buy > 0), total_sells = sum(sell > 0))]
## total_buys total_sells
## 1: 4 4

关于r - R data.table 中的分组计数聚合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24914695/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com