R中的数学动作序列除以组

时间:2018-08-27 16:09:11

标签: r dplyr data.table plyr

我有数据。这里的例子

 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.

然后我需要求和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。 还有另一种变体: 如果对于SalesProperty的ispromo的一个类别,则仅为零或负值 那么X6计算为X6 = 0-x4。 这里的数据 就像我的示例一样,and of cource可以同时是两个变体。

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

1 个答案:

答案 0 :(得分:2)

library(dplyr)

mydat %>% 
  group_by(ItemRelation, DocumentNum) %>% 
  summarise(X1 = sum(SaleCount[SaleCount > 0 & IsPromo == 0]), 
            X2 = sum(SaleCount[SaleCount < 0 & IsPromo == 0]), 
            X3 = X1 + X2, 
            X4 = X3/sum(SaleCount > 0 & IsPromo == 0),
            X5 = sum(SaleCount[IsPromo == 1]),
            X6 = X5/sum(SaleCount > 0 & IsPromo == 1) - 
                 X3/sum(SaleCount > 0 & IsPromo == 0)*
                 sum(SaleCount > 0 & IsPromo == 1)) %>% 
  ungroup()

# # A tibble: 2 x 8
#   ItemRelation DocumentNum    X1    X2    X3    X4    X5    X6
#          <int>       <int> <int> <int> <int> <dbl> <int> <dbl>
# 1        11627        3271   157   -54   103  12.9   104 -59.9
# 2        11628        3270   157   -54   103  12.9   104 -59.9

如您所见,此过程的关键是能够使用适当的值子集来sumSaleCount。例如:sum(SaleCount[SaleCount > 0 & IsPromo == 0])仅对正数sumSaleCount等于IsPromo的情况计算0

以类似的方式,我们可以使用sum(SaleCount > 0 & IsPromo == 0)来对SaleCountIsPromo等于0的观测值进行计数,因为我们得到了{{1 }}的sumTRUE值的(逻辑)向量。

要进行编辑,请尝试以下操作:

FALSE