根据其他列中最早成员的值对整个组重新编码

时间:2018-09-24 12:23:37

标签: r dplyr

我想根据每个组中最早成员的另一列的值来重新编码两列,以指示整个组的状态(x1或x2等于3或0)。

在下面的示例中,x1(x2)是每个组内的key1(key2)的总和(每人始终有三个值/输入)。但是,我只希望每个组都有x1> 0或x2> 0。在那些只有key1 = 1的人和key2 = 1的人(因此x1 = 3 AND x2 = 3)的人群中,年龄最大的应该决定。如果最老的人有key1 = 1和key2 = 0,例如在A组中,则整个组的x1应该是3,x2应该是0,依此类推。

可复制的示例:

id <- c("A11", "A12", "A13", "A21", "A22", "A23", "B11", "B12", "B13", "C11", "C12", "C13", "C21", "C22", "C23", "D11", "D12", "D13", "D21", "D22", "D23", "E11", "E12", "E13", "E21", "E22", "E23")
group <- c("A","A","A","A","A","A","B","B","B","C","C","C","C","C","C","D","D","D","D","D","D","E","E","E","E","E","E")
imputation <- c(rep(1:3, 9))
age <- c(45,45,45,17,17,17,20,20,20,70,70,70,60,60,60,25,25,25,30,30,30,28,28,28,34,34,34)
key1 <- c(1,1,1,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,0,0,0,1,1,1,0,0,0)
key2 <- c(0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0)
x1 <- c(3,3,3,3,3,3,0,0,0,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3)
x2 <- c(3,3,3,3,3,3,0,0,0,3,3,3,3,3,3,3,3,3,3,3,3,0,0,0,0,0,0)
test <- data.frame(id, group, imputation, age, key1, key2, x1, x2)

应重新编码x1和x2的子集:

 > test %>% group_by(group) %>% filter(x1==x2 & x1>0 | x1==x2 & x2>0)
 # A tibble: 18 x 8
 # Groups:   group [3]
   id    group imputation   age  key1  key2    x1    x2
   <fct> <fct>      <int> <dbl> <dbl> <dbl> <dbl> <dbl>
 1 A11   A              1    45     1     0     3     3
 2 A12   A              2    45     1     0     3     3
 3 A13   A              3    45     1     0     3     3
 4 A21   A              1    17     0     1     3     3
 5 A22   A              2    17     0     1     3     3
 6 A23   A              3    17     0     1     3     3
 7 C11   C              1    70     0     1     3     3
 8 C12   C              2    70     0     1     3     3
 9 C13   C              3    70     0     1     3     3
10 C21   C              1    60     1     0     3     3
11 C22   C              2    60     1     0     3     3
12 C23   C              3    60     1     0     3     3
13 D11   D              1    25     1     0     3     3
14 D12   D              2    25     1     0     3     3
15 D13   D              3    25     1     0     3     3
16 D21   D              1    30     0     1     3     3
17 D22   D              2    30     0     1     3     3
18 D23   D              3    30     0     1     3     3

输出应为:

    id group imputation age key1 key2 x1 x2
1  A11     A          1  45    1    0  3  0
2  A12     A          2  45    1    0  3  0
3  A13     A          3  45    1    0  3  0
4  A21     A          1  17    0    1  3  0
5  A22     A          2  17    0    1  3  0
6  A23     A          3  17    0    1  3  0
7  C11     C          1  70    0    1  0  3
8  C12     C          2  70    0    1  0  3
9  C13     C          3  70    0    1  0  3
10 C21     C          1  60    1    0  0  3
11 C22     C          2  60    1    0  0  3
12 C23     C          3  60    1    0  0  3
13 D11     D          1  25    1    0  0  3
14 D12     D          2  25    1    0  0  3
15 D13     D          3  25    1    0  0  3
16 D21     D          1  30    0    1  0  3
17 D22     D          2  30    0    1  0  3
18 D23     D          3  30    0    1  0  3

我想可以通过组合group_by,filter,mutate和ifelse来完成,但是我还没有弄清楚。但是,重要的是它包含过滤器或类似的东西,因为对x1==x2 & x1>0 | x1==x2 & x2>0的观察只是我数据帧的一个子集。

1 个答案:

答案 0 :(得分:0)

在每个group中,您可以将uniqueage的{​​{1}}值与key1为{{1} {1}}为1,并相应更新uniqueage

key2