【发布时间】:2018-08-27 16:09:11
【问题描述】:
我有数据。这里的例子
mydat=structure(list(ItemRelation = c(11628L, 11628L, 11628L, 11628L,
11628L, 11628L, 11628L, 11628L, 11628L, 11628L, 11628L, 11628L,
11628L, 11628L, 11628L, 11628L, 11628L, 11628L, 11628L, 11628L,
11628L, 11628L, 11628L, 11628L, 11628L, 11628L, 11628L, 11628L,
11628L, 11628L, 11628L, 11628L, 11628L, 11628L, 11628L, 11628L,
11628L, 11628L, 11628L, 11628L, 11628L, 11628L, 11628L, 11628L,
11628L, 11628L, 11628L, 11628L, 11628L, 11628L, 11628L, 11628L,
11628L, 11628L, 11628L, 11628L, 11628L, 11628L, 11628L, 11628L,
11628L, 11628L, 11628L, 11628L, 11628L, 11628L, 11627L, 11627L,
11627L, 11627L, 11627L, 11627L, 11627L, 11627L, 11627L, 11627L,
11627L, 11627L, 11627L, 11627L, 11627L, 11627L, 11627L, 11627L,
11627L, 11627L, 11627L, 11627L, 11627L, 11627L, 11627L, 11627L,
11627L, 11627L, 11627L, 11627L, 11627L, 11627L, 11627L, 11627L,
11627L, 11627L, 11627L, 11627L, 11627L, 11627L, 11627L, 11627L,
11627L, 11627L, 11627L, 11627L, 11627L, 11627L, 11627L, 11627L,
11627L, 11627L, 11627L, 11627L, 11627L, 11627L, 11627L, 11627L,
11627L, 11627L, 11627L, 11627L, 11627L, 11627L, 11627L, 11627L
), SaleCount = c(0L, 0L, 6L, 0L, 38L, -14L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 33L, 0L, -10L, -2L, 0L, 22L, -4L, 0L, 0L, -5L, 3L, 0L,
28L, -14L, 0L, 0L, 0L, 0L, 0L, 21L, -5L, 0L, 0L, 0L, 0L, 0L,
32L, -8L, 6L, 0L, 0L, 0L, 0L, 33L, -7L, 0L, 0L, 0L, 3L, -3L,
47L, -22L, 0L, 0L, 0L, 0L, 0L, 26L, -3L, 0L, 0L, 0L, 6L, 0L,
0L, 6L, 0L, 38L, -14L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 33L, 0L, -10L,
-2L, 0L, 22L, -4L, 0L, 0L, -5L, 3L, 0L, 28L, -14L, 0L, 0L, 0L,
0L, 0L, 21L, -5L, 0L, 0L, 0L, 0L, 0L, 32L, -8L, 6L, 0L, 0L, 0L,
0L, 33L, -7L, 0L, 0L, 0L, 3L, -3L, 47L, -22L, 0L, 0L, 0L, 0L,
0L, 26L, -3L, 0L, 0L, 0L, 6L), DocumentNum = c(3270L, 3270L,
3270L, 3270L, 3270L, 3270L, 3270L, 3270L, 3270L, 3270L, 3270L,
3270L, 3270L, 3270L, 3270L, 3270L, 3270L, 3270L, 3270L, 3270L,
3270L, 3270L, 3270L, 3270L, 3270L, 3270L, 3270L, 3270L, 3270L,
3270L, 3270L, 3270L, 3270L, 3270L, 3270L, 3270L, 3270L, 3270L,
3270L, 3270L, 3270L, 3270L, 3270L, 3270L, 3270L, 3270L, 3270L,
3270L, 3270L, 3270L, 3270L, 3270L, 3270L, 3270L, 3270L, 3270L,
3270L, 3270L, 3270L, 3270L, 3270L, 3270L, 3270L, 3270L, 3270L,
3270L, 3271L, 3271L, 3271L, 3271L, 3271L, 3271L, 3271L, 3271L,
3271L, 3271L, 3271L, 3271L, 3271L, 3271L, 3271L, 3271L, 3271L,
3271L, 3271L, 3271L, 3271L, 3271L, 3271L, 3271L, 3271L, 3271L,
3271L, 3271L, 3271L, 3271L, 3271L, 3271L, 3271L, 3271L, 3271L,
3271L, 3271L, 3271L, 3271L, 3271L, 3271L, 3271L, 3271L, 3271L,
3271L, 3271L, 3271L, 3271L, 3271L, 3271L, 3271L, 3271L, 3271L,
3271L, 3271L, 3271L, 3271L, 3271L, 3271L, 3271L, 3271L, 3271L,
3271L, 3271L, 3271L, 3271L), IsPromo = c(0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 0L, 0L)), .Names = c("ItemRelation",
"SaleCount", "DocumentNum", "IsPromo"), class = "data.frame", row.names = c(NA,
-132L))
数据包含两组按 ItemRelation+DocumentNum 列。
11628 3270
11627 3271
有 Ispromo 专栏。它只能取两个值
0 或 1。
所以我需要通过 SaleCount 获得零类 Ispromo 的非负值或零值的总和。 Only 正值的总和。
在这种情况下
6 38 33 22 3 28 21 6
sum=157.
然后我需要得到 sum only 的负值
-14
-10
-2
-4
-5
-14
-5
sum=-54
然后我必须添加这两个值!
157+-54=103 之后,我需要 103 除以正值的总数。
这里只有8个正值。 103/8=12,875。对于 ispromo 列的零类。
对于第一类 Ispromo
通过 salescount 我需要得到所有值的总和以及正负数。
32
-8
6
33
-7
3
-3
47
-22
26
-3
sum=104
然后这个结果我需要除以总计数正值。
是6
32
6
33
3
47
26
104/6=17,33333333
以及最终结果。从这个值(17,33333333) 我需要提取Zero category of ispromo when we 103 divided by the total number of positive values.的结果
*103/8=12,875*
并乘以第一类 ispromo 的正值计数
在我们的例子中是6
17,33333333-(12,875*6)= -59,91666667
必须对每个组进行此数学运算
11628 3270
11627 3271
怎么做? 预期输出
ItemRelation DocumentNum Ispromo_by_SaleCount_sum_of_not_negative_or_zero_value for_negative_value
1 11628 3270 157 -54
2 11627 3271 157 -54
substract_positive_and_negative Ispromo_by_salescount_i_need_get_sum_all_values_and_positive_and_negative
1 103 104
2 103 104
divide_on_total_count_positive_value._It_is_5 end_result
1 12.875 -59.9
2 12.875 -59.9
或输入预期结果
expect=sstructure(list(ItemRelation = c(11628L, 11627L), DocumentNum = 3270:3271,
Ispromo_by_SaleCount_sum_of_not_negative_or_zero_value = c(157L,
157L), for_negative_value = c(-54L, -54L), substract_positive_and_negative = c(103L,
103L), Ispromo_by_salescount_i_need_get_sum_all_values_and_positive_and_negative = c(104L,
104L), divide_on_total_count_positive_value._It_is_5 = c(12.875,
12.875), end_result = c(-59.9, -59.9)), .Names = c("ItemRelation",
"DocumentNum", "Ispromo_by_SaleCount_sum_of_not_negative_or_zero_value",
"for_negative_value", "substract_positive_and_negative", "Ispromo_by_salescount_i_need_get_sum_all_values_and_positive_and_negative",
"divide_on_total_count_positive_value._It_is_5", "end_result"
), class = "data.frame", row.names = c(NA, -2L))
使用特定数据编辑
如果 SaleCount 的零类别 ispromo 我只有零值或负值,那么 x4 必须默认为 =0。 还有另一种变体: 如果 SaleCount 的一类 ispromo 只有零值或负值 然后 X6 计算为 X6=0-x4。 这里的数据 和当然可以同时是两个变体,就像我的例子一样。
mydat=structure(list(ItemRelation = c(11709L, 11709L, 11709L, 11709L,
11709L, 11709L, 11709L, 11709L, 11709L, 11709L, 11709L, 11709L,
11709L, 11709L, 11709L, 11709L, 11709L, 11709L, 11709L, 11709L,
11709L, 11709L, 11709L, 11709L, 11709L, 11709L, 11709L, 11709L,
11709L, 11709L, 11709L, 11709L, 11709L, 11709L, 11709L, 11709L,
11709L, 11709L, 11709L, 11709L, 11709L, 11709L, 11709L, 11709L,
11709L, 11709L, 11709L, 11709L, 11709L, 11709L, 11709L, 11709L
), SaleCount = c(0L, 0L, -1L, 0L, 0L, 0L, -2L, 0L, 0L, -1L, 0L,
0L, 0L, -1L, -1L, 0L, 0L, -1L, 0L, 0L, 0L, 0L, -1L, 0L, 0L, 0L,
0L, 0L, 0L, -2L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, -1L, 0L, 0L,
0L, -1L, 0L, 0L, 0L, 1L, -2L, 0L, 0L, 0L, 0L), DocumentNum = c(1002L,
1002L, 1002L, 1002L, 1002L, 1002L, 1002L, 1002L, 1002L, 1002L,
1002L, 1002L, 1002L, 1002L, 1002L, 1002L, 1002L, 1002L, 1002L,
1002L, 1002L, 1002L, 1002L, 1002L, 1002L, 1002L, 1002L, 1002L,
1002L, 1002L, 1002L, 1002L, 1002L, 1002L, 1002L, 1002L, 1002L,
1002L, 1002L, 1002L, 1002L, 1002L, 1002L, 1002L, 1002L, 1002L,
1002L, 1002L, 1002L, 1002L, 1002L, 1002L), IsPromo = c(0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L)), .Names = c("ItemRelation", "SaleCount", "DocumentNum",
"IsPromo"), class = "data.frame", row.names = c(NA, -52L))
这里输出
ItemRelation DocumentNum CalendarYear X1 X2 X3 X4 X5 X6
1 11709 1002 2018 any value any value any value 0 any value 0-x4=0
【问题讨论】:
-
请再次检查您提供的数据和您的计算。似乎你忘记了价值观。您在计算中忽略了
3和-2。然后,您必须解释如何以6结束“正值总数”,然后您说5忽略值33... -
@AntoniosK,我昨天想问你,但我的互联网出了点问题。谢谢你的通知,我编辑了帖子。我真的忘记了任何价值粘贴。请检查一下。我更新了帖子。有很多价值观,有些是我无意中错过的。
-
现在有意义了 :) 我在下面发布了一个解决方案。
标签: r dplyr data.table plyr