【发布时间】:2021-11-21 12:51:12
【问题描述】:
我有一个如下所示的 data.table:
| ID1 | ID2 | ID3 | ID4 | subtotal | total |
|---|---|---|---|---|---|
| 001 | 001 | 001 | 001 | 10 | 100 |
| 001 | 001 | 001 | 002 | 5 | 20 |
| 001 | 002 | 001 | 001 | 10 | 200 |
然后我可以使用闪亮选择要分组的 ID,例如 ID1 到 ID3:
| ID1 | ID2 | ID3 | subtotal | total |
|---|---|---|---|---|
| 001 | 001 | 001 | 15 | 120 |
| 001 | 002 | 001 | 10 | 200 |
如您所见,该表的第一行是第一个表的前两行的总和。
然后我计算百分比,该列将自动放在最后:
| ID1 | ID2 | ID3 | subtotal | total | percentage |
|---|---|---|---|---|---|
| 001 | 001 | 001 | 15 | 120 | 12.5 |
| 001 | 002 | 001 | 10 | 200 | 5 |
但是,我希望在 ID 之后看到此列。
我尝试使用setcolorder,但列可能会因选择的 ID 而异。使用的 ID 存储在我尝试使用的向量中:
dt[, .(vector, percentage, subtotal, total)]
和:
dt[, c(vector, "percentage", "subtotal", "total")]
但这两种方法都不起作用
供参考(请记住,它应该适用于任何 ID 组合):
dput(vector)
c("ID1", "ID2", "ID3")
【问题讨论】:
-
这能回答你的问题吗? Overlap join with start and end positions
标签: r shiny data.table