dplyr mutuate仅在不同的列==特定值时添加新列值

时间:2018-03-08 23:35:32

标签: r dplyr mutate

我有一个具有以下结构的数据框(摘要示例,不是实际的)

dput(df1)
structure(list(MedID = c(111, 111, 111, 111, 111, 111, 222, 222, 
222, 222, 222), Service = structure(c(1L, 1L, 2L, 1L, 1L, 3L, 
3L, 2L, 1L, 1L, 3L), .Label = c("Acute care", "Ext care", "Outpt 
care"), class = "factor"), AdmitDate = structure(c(16832, 16861, 
16892, 16922, 16953, 16983, 17181, 17212, 17240, 17271, 17301), class 
= "Date"), Flag = c(0, 0, 99, 0, 0, 0, 0, 99, 0, 0, 0)), .Names = 
c("MedID", "Service", "AdmitDate", "Flag"), row.names = c(NA, -11L), 
class = "data.frame")
> df1
    MedID  Service   AdmitDate  Flag
1    111 Acute care 2016-02-01    0
2    111 Acute care 2016-03-01    0
3    111   Ext care 2016-04-01   99
4    111 Acute care 2016-05-01    0
5    111 Acute care 2016-06-01    0
6    111 Outpt care 2016-07-01    0
7    222 Outpt care 2017-01-15    0
8    222   Ext care 2017-02-15   99
9    222 Acute care 2017-03-15    0
10   222 Acute care 2017-04-15    0
11   222 Outpt care 2017-05-15    0

我希望使用dplyr,group_by(MedID)和mutate在新数据框中添加一个列(让我们在df2中将其称为Flag2),以便在每个患者(MedID)中df2 $ Flag2列== 1 用于该唯一MedID 中的每个后续行,但仅在df1 $ Flag列== 99之后,否则df2 $ Flag2列获得0.如果df1 $ Flag ==,我可以根据需要对其进行编码在MedID的第一行中为99,但是否则我的代码在df2 $ Flag2中仅在df1 $ Flag == 99,的行中生成1,它为给定的所有行生成1 MedID,其中df1 $ Flag == 99.所需的输出为:

dput(df2)
structure(list(MedID = c(111, 111, 111, 111, 111, 111, 222, 222, 
222, 222, 222), Service = structure(c(1L, 1L, 2L, 1L, 1L, 3L, 
3L, 2L, 1L, 1L, 3L), .Label = c("Acute care", "Ext care", "Outpt 
care"), class = "factor"), AdmitDate = structure(c(16832, 16861, 
16892,16922, 16953, 16983, 17181, 17212, 17240, 17271, 17301), class 
= "Date"),Flag = c(0, 0, 99, 0, 0, 0, 0, 99, 0, 0, 0), Flag2 = c(0, 
0, 1, 1, 1, 1, 0, 1, 1, 1, 1)), .Names = c("MedID", "Service", 
"AdmitDate", "Flag", "Flag2"), row.names = c(NA, -11L), class = 
"data.frame")
> df2
    MedID    Service  AdmitDate Flag Flag2
1    111 Acute care 2016-02-01    0     0
2    111 Acute care 2016-03-01    0     0
3    111   Ext care 2016-04-01   99     1
4    111 Acute care 2016-05-01    0     1
5    111 Acute care 2016-06-01    0     1
6    111 Outpt care 2016-07-01    0     1
7    222 Outpt care 2017-01-15    0     0
8    222   Ext care 2017-02-15   99     1
9    222 Acute care 2017-03-15    0     1
10   222 Acute care 2017-04-15    0     1
11   222 Outpt care 2017-05-15    0     1

这是代码的片段示例,但由于它没有正确执行,因此不完整...我是否需要将mutate嵌套在For循环中,这看起来像是混合的R编码? :(注意:df1 $ Flag每个MedID只能== 99次,我觉得应该会更容易。

`df2 <- df1 %>% `
    `group_by(MedID) %>%`
    `mutate(Flag2 = ifelse(df1$Flag == 99, 1, 0))`  

1 个答案:

答案 0 :(得分:0)

一种解决方案可能是使用fill中的tidyr。方法是首先添加Flag2并为1分配Flag == 99,否则为NA

现在在Flag2列中向下填充行。最后将所有NA替换为0。

  library(tidyverse)
  df1 %>%
  group_by(MedID) %>%
  mutate(Flag2 = ifelse(Flag == 99, 1L, NA)) %>%
    fill(Flag2) %>%
    mutate(Flag2 = ifelse(is.na(Flag2), 0L, Flag2))

虽然OP没有提及,但如果AdmitDate预计在匹配Flag == 99之后决定哪一行,那么应该在上面的代码中添加安排。

  df1 %>%
  group_by(MedID) %>%
  mutate(Flag2 = ifelse(Flag == 99, 1L, NA)) %>%
  arrange(AdmitDate) %>%
    fill(Flag2) %>%
    mutate(Flag2 = ifelse(is.na(Flag2), 0L, Flag2))