mercredi 10 octobre 2018

Make dplyr::if_else() return a NA value

Using dplyr::if_else() on a date, I want to return a NA - but there is no NA_date_ or NA_double_ in R (that I know of - or listed here)? How can I execute the following code:

df %>%
  group_by(rootnum, episode) %>%
  summarise(ep_adm = min(adm, na.rm = T),
            ep_sepa = if_else(all(is.na(.$sepa)),
                             if_else(all(is.na(.$adm)), NA_integer_ , max(adm, na.rm = T)),
                             max(sepa, na.rm = T)))

without the error?

Error in summarise_impl(.data, dots) : Evaluation error: false must be type integer, not double.

data

df <- structure(list(rootnum = c(1, 1, 1, 1, 1, 2), episode = c(1L, 
1L, 2L, 3L, 4L, 2L), adm = structure(c(11866, 11866, 11924, 12229, 
13613, 15517), class = "Date"), sepa = structure(c(NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), class = "Date")), row.names = c(NA, 
-6L), class = c("tbl_df", "tbl", "data.frame"))

Aucun commentaire:

Enregistrer un commentaire