How to return NA and not -Inf using max() across many rows in a large data frame?

Viewed 36

I have a large dataset, that looks someting like:

  mr_daterd  mr_daterd_fu1_v1 mr_daterd_fu1_v2 mr_daterd_fu1_v2_v3 date_contact_1 date_contact_2
  <date>     <date>           <date>           <date>              <date>         <date>        
1 2007-09-07 NA               2009-01-23       2010-02-05          2008-10-14     2009-03-03    
2 2013-05-23 NA               NA               NA                  2014-03-17     NA            
3 2012-11-12 NA               NA               NA                  NA             NA            
4 2013-07-10 NA               NA               NA                  NA             NA 

I want to identify the latest date like

df %>%  mutate(Last_MRI_date = max(c_across(starts_with("mr_daterd")), na.rm = TRUE),
               Last_clin_date = max(c_across(starts_with("date_contact")), na.rm = TRUE)) %>%
        select(Last_MRI_date, Last_clin_date) 

But I get -Inf instead of NA:

  Last_MRI_date Last_clin_date
  <date>        <date>        
1 2010-02-05    2016-01-26    
2 2013-05-23    2014-03-17    
3 2012-11-12    -Inf          
4 2013-07-10    -Inf   

I realize this has been discussed previously; yet, I cannot find a solution to integrate in my dplyr pipe. How can I apply the above syntax, but with NA returned instead of -Inf?

Data

df <- structure(list(mr_daterd = structure(c(13763, 15848, 15656, 15896
), class = "Date"), mr_daterd_fu1_v1 = structure(c(NA_real_, 
NA_real_, NA_real_, NA_real_), class = "Date"), mr_daterd_fu1_v2 = structure(c(14267, 
NA, NA, NA), class = "Date"), mr_daterd_fu1_v2_v3 = structure(c(14645, 
NA, NA, NA), class = "Date"), date_contact_1 = structure(c(14166, 
16146, NA, NA), class = "Date"), date_contact_2 = structure(c(14306, 
NA, NA, NA), class = "Date"), date_contact_3 = structure(c(14621, 
NA, NA, NA), class = "Date"), date_contact_4 = structure(c(15019, 
NA, NA, NA), class = "Date"), date_contact_5 = structure(c(15397, 
NA, NA, NA), class = "Date"), date_contact_6 = structure(c(15684, 
NA, NA, NA), class = "Date"), date_contact_7 = structure(c(16468, 
NA, NA, NA), class = "Date"), date_contact_8 = structure(c(16826, 
NA, NA, NA), class = "Date"), date_contact_9 = structure(c(NA_real_, 
NA_real_, NA_real_, NA_real_), class = "Date"), date_contact_10 = structure(c(NA_real_, 
NA_real_, NA_real_, NA_real_), class = "Date"), date_contact_11 = structure(c(NA_real_, 
NA_real_, NA_real_, NA_real_), class = "Date"), date_contact_12 = structure(c(NA_real_, 
NA_real_, NA_real_, NA_real_), class = "Date"), date_contact_13 = structure(c(NA_real_, 
NA_real_, NA_real_, NA_real_), class = "Date")), class = c("rowwise_df", 
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -4L), groups = structure(list(
    .rows = structure(list(1L, 2L, 3L, 4L), ptype = integer(0), class = c("vctrs_list_of", 
    "vctrs_vctr", "list"))), row.names = c(NA, -4L), class = c("tbl_df", 
"tbl", "data.frame")))
0 Answers
Related