说,我有数据集。
df=structure(list(ItemRelation = c(13250L, 13250L, 13250L, 13250L,
13250L, 13250L, 13250L, 13250L, 13250L, 13250L, 13250L, 13250L,
1300L, 1300L, 1300L, 1300L, 1300L, 1300L, 1300L, 1300L, 1300L,
1300L, 1300L, 1300L), SaleCount = c(354L, 679L, 397L, 473L, 614L,
404L, 127L, 434L, 786L, 127L, 434L, 786L, 354L, 679L, 397L, 473L,
614L, 404L, 127L, 434L, 786L, 127L, 434L, 786L), DocumentNum = c(336L,
336L, 336L, 336L, 336L, 336L, 336L, 336L, 336L, 336L, 336L, 336L,
335L, 335L, 335L, 335L, 335L, 335L, 335L, 335L, 335L, 335L, 335L,
335L), IsPromo = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 1L,
1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 1L)), .Names = c("ItemRelation",
"SaleCount", "DocumentNum", "IsPromo"), class = "data.frame", row.names = c(NA,
-24L))
存在变量ispromo。它仅取值0和1。 因此,我必须按销售额分别为每个组计算总和,但仅针对ispromo的1类。 组是ItemRelation + SaleCount + DocumentNum
我该怎么办?
所需的输出
ItemRelation DocumentNum sum1
13250 336 1347
1300 335 1347
答案 0 :(得分:2)
使用dplyr:
library(dplyr)
df %>%
group_by(ItemRelation, DocumentNum) %>%
filter(IsPromo == 1) %>%
summarise(sum1 = sum(SaleCount))
# A tibble: 2 x 3
# Groups: ItemRelation [?]
ItemRelation DocumentNum sum1
<int> <int> <int>
1 1300 335 1347
2 13250 336 1347
答案 1 :(得分:0)
这是使用onClick
aggregate