我有这样的数据
dt <- data.table(group = rep(1:3,100),
price = rep(1:10, 30),
currency = rep(1:2,150)
)
> dt
group price currency
1: 1 1 1
2: 2 2 2
3: 3 3 1
4: 1 4 2
5: 2 5 1
---
296: 2 6 2
297: 3 7 1
298: 1 8 2
299: 2 9 1
300: 3 10 2
基本上对于每个组我都有一些以收费的价格收费的物品。有些商品以货币1定价,有些商品以货币2定价。我可以轻松地为集团收入总和
dt[, .(varname="total revenue",
value = sum(price)
),
by = list(group)
]
group varname value
1: 1 total revenue 550
2: 2 total revenue 550
3: 3 total revenue 550
我也可以很容易地对该组和货币进行总结
dt[, .(varname="total revenue",
value = sum(price)
),
by = list(group,currency)
]
group currency varname value
1: 1 1 total revenue 250
2: 2 2 total revenue 300
3: 3 1 total revenue 250
4: 1 2 total revenue 300
5: 2 1 total revenue 250
6: 3 2 total revenue 300
但我真正喜欢的是拥有一个包含group的数据表,varname包含货币名称和总和值。我可以用
来计算我想要的东西dt[, .(varname=paste("total revenue",currency),
value = sum(price)
),
by = list(group,currency)
]
group currency varname value
1: 1 1 total revenue 1 250
2: 2 2 total revenue 2 300
3: 3 1 total revenue 1 250
4: 1 2 total revenue 2 300
5: 2 1 total revenue 1 250
6: 3 2 total revenue 2 300
但理想情况下,我希望摆脱货币列,因为该值现在存在于变量名称中。我可以用链接来实现这个目标
x <- dt[, .(varname=paste("total revenue",currency),
value = sum(price)
),
by = list(group,currency)
][, currency:=NULL]
> x
group varname value
1: 1 total revenue 1 250
2: 2 total revenue 2 300
3: 3 total revenue 1 250
4: 1 total revenue 2 300
5: 2 total revenue 1 250
6: 3 total revenue 2 300
但我不确定这是否是正确的&#34;用数据表实现这一目标的方法。我或许认为可能有一种方法可以用一个命令来做,即不使用链接。我不反对链接只是想知道是否有使用data.table语法的替代方法。
任何意见/建议表示赞赏
答案 0 :(得分:2)
我认为链接没有问题,但它看起来像这样有效:
dt[, .(value = sum(price)
),
by = list(group,varname = paste("total revenue",currency))
]
希望有所帮助!
答案 1 :(得分:0)
使用dplyr-package轻松完成这样的任务:
library(dplyr)
dt %>%
group_by(group, currency) %>%
summarise(total = sum(price))
如果您要重命名price-colum,可以添加其他功能:
%>% mutate(currency = paste('total revenue', currency))