按逻辑条件按组替换值

时间:2018-11-02 14:59:49

标签: r dplyr data.table

此处数据集

 mydat=structure(list(shop_code = structure(c(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), .Label = "00664НСК", class = "factor"), product_id = 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
), doc_date = structure(1:25, .Label = c("2015-01-03 00:00:00.000", 
"2015-01-05 00:00:00.000", "2015-01-06 00:00:00.000", "2015-01-07 00:00:00.000", 
"2015-01-08 00:00:00.000", "2015-01-09 00:00:00.000", "2015-01-10 00:00:00.000", 
"2015-01-11 00:00:00.000", "2015-01-12 00:00:00.000", "2015-01-13 00:00:00.000", 
"2015-01-14 00:00:00.000", "2015-01-15 00:00:00.000", "2015-01-16 00:00:00.000", 
"2015-01-17 00:00:00.000", "2015-01-18 00:00:00.000", "2015-01-19 00:00:00.000", 
"2015-01-20 00:00:00.000", "2015-01-21 00:00:00.000", "2015-01-24 00:00:00.000", 
"2015-01-25 00:00:00.000", "2015-01-26 00:00:00.000", "2015-01-27 00:00:00.000", 
"2015-01-28 00:00:00.000", "2015-01-29 00:00:00.000", "2015-01-30 00:00:00.000"
), class = "factor"), ship_count = c(12L, 7L, 24L, 18L, 12L, 
18L, 6L, 6L, 6L, 18L, 6L, 18L, 12L, 18L, 12L, 10L, 24L, 6L, 6L, 
6L, 10L, 6L, 10L, 70L, 100L), mark_1 = c(1L, 1L, 0L, 1L, 1L, 
0L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 0L, 
1L, 1L, 0L, 1L), outputer = c(8L, 8L, 8L, 8L, 8L, 8L, 6L, 6L, 
6L, 12L, 6L, 12L, 12L, 12L, 12L, 10L, 12L, 6L, 6L, 6L, 10L, 6L, 
10L, 12L, 12L), y = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 
0L, 0L, 1L, 1L, 1L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 1L, 1L)), .Names = c("shop_code", 
"product_id", "doc_date", "ship_count", "mark_1", "outputer", 
"y"), class = "data.frame", row.names = c(NA, -25L))

我需要遵守以下条件: 如果y = 1并且mark1 = 1,则必须将mark1的输出者替换为输出变量中y = 0和mark1 = 1的第一个值。

如果在输出器中用于Y = 0和mark1 = 1的第一个值大于ship_count,则在输出器中留下ship_count的实际值

标记1的输出类别为零,我们不会碰。

此操作必须由ship_code + product_id组完成

所以想要的输出

 shop_code product_id                doc_date ship_count mark_1 outputer   y
1   00664НСК      11628 2015-01-03 00:00:00.000        12      1       *12 1
2   00664НСК      11628 2015-01-05 00:00:00.000          7      1        *7 1
3   00664НСК      11628 2015-01-06 00:00:00.000         24      0       24 1
4   00664НСК      11628 2015-01-07 00:00:00.000         18      1       *12 1
5   00664НСК      11628 2015-01-08 00:00:00.000         12      1       *12 1
6   00664НСК      11628 2015-01-09 00:00:00.000         18      0       18 1
7   00664НСК      11628 2015-01-10 00:00:00.000          6      0        6 1
8   00664НСК      11628 2015-01-11 00:00:00.000          6      1        6 1
9   00664НСК      11628 2015-01-12 00:00:00.000          6      1        6 1
10  00664НСК      11628 2015-01-13 00:00:00.000         18      1       *12 0
11  00664НСК      11628 2015-01-14 00:00:00.000          6      1        6 0
12  00664НСК      11628 2015-01-15 00:00:00.000         18      1       12 0
13  00664НСК      11628 2015-01-16 00:00:00.000         12      1       *10 1
14  00664НСК      11628 2015-01-17 00:00:00.000         18      1       *10 1
15  00664НСК      11628 2015-01-18 00:00:00.000         12      1       *10 1
16  00664НСК      11628 2015-01-19 00:00:00.000         10      1       10 0
17  00664НСК      11628 2015-01-20 00:00:00.000         24      1       12 0
18  00664НСК      11628 2015-01-21 00:00:00.000          6      1        6 0
19  00664НСК      11628 2015-01-24 00:00:00.000          6      1        6 0
20  00664НСК      11628 2015-01-25 00:00:00.000          6      0        6 0
21  00664НСК      11628 2015-01-26 00:00:00.000         10      0       10 1
22  00664НСК      11628 2015-01-27 00:00:00.000          6      1        6 1
23  00664НСК      11628 2015-01-28 00:00:00.000         20      1       *12 0
24  00664НСК      11628 2015-01-29 00:00:00.000         70      1       12 0
25  00664НСК      11628 2015-01-30 00:00:00.000        100      1       12 1

*标记为替换值

1 个答案:

答案 0 :(得分:1)

我的看法:

<item name="android:maxLines">1</item>