sutsabs
sutsabs

Reputation: 427

imputing variable if value is NA dplyr

I am trying to impute a variable, dt_drug_end if dt_drug_end is missing(NA).

The logic is:
1.If it is part of group of line_number then groupby line_number where dt_drug_end = max(dt_drug_end).
2. else, impute the end date as the dt_drug_start if start date is avaiable.

My code is not imputing the end date as the start date when max(dt_drug_end) is NA. Even when I use the logic case_when( is.na(dt_drug_end) & is.na(max_dt_drug_end) ~ dt_drug_start) the dt_end_date is NA instead of matching the 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

Data

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"))

My Code :

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
))

Upvotes: 0

Views: 63

Answers (2)

SEcker
SEcker

Reputation: 417

I think your code is technically ok, but the problem is that running max(dt_drug_end, na.rm = T) returns an infinite value. And this trips your is.na check.

df %>%
  # Create Helper column with dates based on your rules
  group_by(patient_id, 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)
  )

Upvotes: 1

rjen
rjen

Reputation: 1982

An alternative using 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

Upvotes: 0

Related Questions