繁体   English   中英

如果值为 NA,则估算变量 dplyr

[英]imputing variable if value is NA dplyr

如果dt_drug_end缺失(NA),我正在尝试估算一个变量dt_drug_end

逻辑是:
1.如果它是line_number组的一部分,则 groupby line_number where dt_drug_end = max(dt_drug_end)
2. 否则,如果开始日期可用,则将结束日期估算为dt_drug_start

当 max(dt_drug_end) 为 NA 时,我的代码不会将结束日期作为开始日期。 即使我使用逻辑 case_when(is.na(dt_drug_end) & is.na(max_dt_drug_end) ~ dt_drug_start) dt_end_date 是 NA 而不是匹配dt_drug_end

patient_id line_number_raw drug_name           dt_drug_start dt_drug_end reason_discount         trt_diag flag_after_diag_adv
  <chr>      <chr>           <chr>               <date>        <date>      <chr>                   <chr>                  <dbl>
1 0Q97V6R3GI 1               Carboplatin         2013-04-12    2013-04-18  Yes, Comment field      C34.3                      1
2 0Q97V6R3GI 1               Cisplatin           2013-04-12    2013-06-16  Completed Treatment     C34.3                      1
3 0Q97V6R3GI 3               Denosumab           2016-12-06    NA          No                      C34.3                      1
4 0Q97V6R3GI 4               Osimertinib         2018-06-14    NA          No                      C34.3                      1
5 0Q97V6R3GI 1               Pemetrexed Disodium 2013-04-12    2013-06-16  Completed Treatment     C34.3                      1
6 0Q97V6R3GI 3               Erlotinib           2015-06-02    2018-05-30  Yes, due to progression C34.9                      1

数据

df <- structure(
list(
    patient_id = c("0Q97V6R3GI","0Q97V6R3GI","0Q97V6R3GI","0Q97V6R3GI","0Q97V6R3GI","0Q97V6R3GI"),
    line_number_raw = c("1","1", "3", "4", "1", "3"),
    drug_name = c(
        "Carboplatin",
        "Cisplatin",
        "Denosumab",
        "Osimertinib",
        "Pemetrexed Disodium",
        "Erlotinib"
    ),
    dt_drug_start = structure(c(15807, 15807, 17141, 17696, 15807,16588), class = "Date"),
    dt_drug_end = structure(c(15813, 15872,NA, NA, 15872, 17681), class = "Date"),
    reason_discount = c(
        "Yes, Comment field",
        "Completed Treatment",
        "No",
        "No",
        "Completed Treatment",
        "Yes, due to progression"
    ),
    trt_diag = c("C34.3", "C34.3", "C34.3", "C34.3", "C34.3", "C34.9"),
    flag_after_diag_adv = c(1, 1, 1, 1, 1, 1)
),
row.names = c(NA,-6L),
class = c("tbl_df", "tbl", "data.frame"))

我的代码:

tb_episode %>% 
group_by(patient_id, line_number_raw) %>% 
mutate(max_dt_drug_end = case_when(
    line_number_raw == "unknown" ~ NA_Date_,
    TRUE ~ max(dt_drug_end, na.rm = T)
)) %>% 
ungroup() %>% 
mutate(dt_drug_end = case_when(
    is.na(dt_drug_end) & !is.na(max_dt_drug_end) ~ max_dt_drug_end,
    is.na(max_dt_drug_end) ~ dt_drug_start,
    TRUE ~ dt_drug_end
))

我认为您的代码在技术上还可以,但问题是运行max(dt_drug_end, na.rm = T)返回一个无限值。 这会让您的is.na检查出错。

df %>%
  # Create Helper column with dates based on your rules
  group_by(line_number_raw) %>% 
  mutate(impute_date = max(dt_drug_end, na.rm = T)) %>% 
  ungroup() %>% 
  mutate(impute_date = if_else(is.infinite(impute_date), lubridate::NA_Date_, impute_date)) %>% 
  mutate(impute_date = if_else(is.na(impute_date), dt_drug_start, impute_date) ) %>% 
  
  # impute
  mutate(dt_drug_end =
  if_else(is.na(dt_drug_end),
          impute_date,
          dt_drug_end)
  )

使用dplyr::rowwise()的替代方法。

library(dplyr)

df %>%
  group_by(line_number_raw) %>%
  mutate(several = n() > 1,
         gMax = max(dt_drug_end, na.rm = T)) %>%
  rowwise() %>%
  mutate(dt_drug_end_X = case_when(is.na(dt_drug_end) & several == T ~ gMax,
                                   is.na(dt_drug_end) & several == F ~ dt_drug_start,
                                   T ~ dt_drug_end)) %>%
  select(starts_with(c('dt', 'line')))

# # A tibble: 6 x 4
# # Rowwise: 
#   dt_drug_start dt_drug_end dt_drug_end_X line_number_raw
#   <date>        <date>      <date>        <chr>          
# 1 2013-04-12    2013-04-18  2013-04-18    1              
# 2 2013-04-12    2013-06-16  2013-06-16    1              
# 3 2016-12-06    NA          2018-05-30    3              
# 4 2018-06-14    NA          2018-06-14    4              
# 5 2013-04-12    2013-06-16  2013-06-16    1              
# 6 2015-06-02    2018-05-30  2018-05-30    3

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM