R语言 对于所有连续NA值,相邻列NA值的末次观察值向后进位

68bkxrlz  于 2023-05-04  发布在  其他
关注(0)|答案(1)|浏览(126)
行号(_N)患者ID开始日期开始_窗口
1零零一2023-04-01 2023-04-012023-04-01 2023-04-01
零零一2023-04-03 2023-04-03不适用
零零一2023-04-05 2023-04-05 2023-04-05不适用
零零一2023-04-06 2023-04-06不适用
零零一2023-04-08 2023-04-08不适用
零零一2023-04-09 2023-04-092023-04-09 2023-04-09
零零一2023-04-11 2023-04-11 2023-04-112023-04-11 2023-04-11 2023-04-11
零零一2023-04-13 2023-04-13 2023-04-13不适用
零零一2023-04-16 2023-04-16 2023-04-16不适用
10个零零一2023-04-18 2023-04-18 2023-04-182023-04-18 2023-04-18 2023-04-18
十一零零二2023-04-02 2023-04-022023-04-02 2023-04-02
十二岁零零二2023-04-04 2023-04-042023-04-04 2023-04-04
十三零零二2023-04-07 2023-04-072023-04-07 2023-04-07
十四岁零零二2023-04-08 2023-04-082023-04-08 2023-04-08
十五岁零零二2023-04-10 2023-04-10 2023-04-10不适用
# Create the data frame
df <- data.frame(
  Row_Num = c(1:15),
  Patient_ID = c(rep(001, 10), rep(002, 5)),
  Start_Date = c("2023-04-01", "2023-04-03", "2023-04-05", "2023-04-06", "2023-04-08",
                 "2023-04-09", "2023-04-11", "2023-04-13", "2023-04-16", "2023-04-18",
                 "2023-04-02", "2023-04-04", "2023-04-07", "2023-04-08", "2023-04-10"),
  Start_Window = c("2023-04-01", NA, NA, NA, NA, "2023-04-09", "2023-04-11", NA, NA, "2023-04-18",
                   "2023-04-02", "2023-04-04", "2023-04-07", "2023-04-08", NA)
)

# Print the table
print(df)

我有上面的表,它是使用上面的可复制的R代码创建的。
我想用“开始日期”的值替换开始窗口中的“NA”。然而,当NA值对于每个患者ID是连续的时,我想要向后携带第一个值。
所以在这个例子中,在第1-5行中,start_window应该是“2023-04-08”。第8-9行start_window应为“2023-04-16”。第15行应为“2023-04-10”。

798qvoo8

798qvoo81#

您可以基于Patient_IDStart_Window的游程长度创建分组ID,对于NA s,则采用该组的最后一个Start_Date值。

library(dplyr)

df %>%
  mutate(temp_id = consecutive_id(Start_Window, Patient_ID)) %>%
  mutate(Start_Window = if_else(is.na(Start_Window), last(Start_Date), Start_Window),
         temp_id = NULL, .by = temp_id)

   Row_Num Patient_ID Start_Date Start_Window
1        1          1 2023-04-01   2023-04-01
2        2          1 2023-04-03   2023-04-08
3        3          1 2023-04-05   2023-04-08
4        4          1 2023-04-06   2023-04-08
5        5          1 2023-04-08   2023-04-08
6        6          1 2023-04-09   2023-04-09
7        7          1 2023-04-11   2023-04-11
8        8          1 2023-04-13   2023-04-16
9        9          1 2023-04-16   2023-04-16
10      10          1 2023-04-18   2023-04-18
11      11          2 2023-04-02   2023-04-02
12      12          2 2023-04-04   2023-04-04
13      13          2 2023-04-07   2023-04-07
14      14          2 2023-04-08   2023-04-08
15      15          2 2023-04-10   2023-04-10

相关问题