Skip to content

no non-missing arguments to max; returning -Inf [2(dplyr/mutate()]

1 message · Muhuri, Pradip (SAMHSA/CBHSQ)

#
Hello,

With dplyr mutate(), the code below creates a new column (oiddate), which is the maximum of the four dates (mrjdate,cocdate, inhdate, haldate).  The code seems to provide the results (presented below) I desired.  But, the issue is that I am getting  the following warning message:  1: In max(13113, NA_real_, 14336, NA_real_, na.rm = TRUE) :   no non-missing arguments to max; returning -Inf 2.

Is this warning message harmful?  Any hints how to tweak the code in order to correct the problem or avoid this message?

Please note that I did not get this warning message when I executed the code on the reproducible example data posted to this forum in the past  and that I am now getting this warning when applying the code on the actual working data file.   Thanks to Arun, Mark and others on this forum for their help with tweaking the code in the past.   Sorry for not providing the reproducible example this time.  

Thanks,

Pradip Muhuri

#################  R script followed by console (log and output) #############
setwd ("H:/R/cis_study")
library(dplyr)
load("xd2012.rdata")
# create a new column of the max date from four dates

 test <- xd2012 %>% 
  rowwise() %>%
  mutate( oidflag= ifelse(mrjflag==1 | cocflag==1 | inhflag==1 | halflag==1, 1, 0),
          oiddate=as.Date(max(mrjdate,cocdate, inhdate, haldate, na.rm=TRUE), origin='1970-01-01')) %>%
  filter(oidflag==1)  %>%
  select( mrjdate, cocdate, inhdate, haldate,  oiddate)
  
head(test)
warnings(2)
    

##########################  below is from the console  ####################
load("xd2012.rdata")
+   rowwise() %>%
+   mutate( oidflag= ifelse(mrjflag==1 | cocflag==1 | inhflag==1 | halflag==1, 1, 0),
+           oiddate=as.Date(max(mrjdate,cocdate, inhdate, haldate, na.rm=TRUE), origin='1970-01-01')) %>%
+   filter(oidflag==1)  %>%
+   select( mrjdate, cocdate, inhdate, haldate,  oiddate)
There were 50 or more warnings (use warnings() to see the first 50)
Source: local data frame [6 x 5]

     mrjdate cocdate    inhdate    haldate    oiddate
1 2003-02-22    <NA> 2006-03-10 2005-09-17 2006-03-10
2 2007-12-07    <NA>       <NA>       <NA> 2007-12-07
3 1994-05-15    <NA>       <NA>       <NA> 1994-05-15
4 2003-04-19    <NA>       <NA>       <NA> 2003-04-19
5 2009-11-13    <NA>       <NA>       <NA> 2009-11-13
6 1973-10-08    <NA>       <NA> 1974-01-04 1974-01-04
Warning messages:
1: In max(13113, NA_real_, 14336, NA_real_, na.rm = TRUE) :
  no non-missing arguments to max; returning -Inf 2
2: In max(13113, NA_real_, 14336, NA_real_, na.rm = TRUE) :
no non-missing arguments to max; returning -Inf 2


-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Muhuri, Pradip (SAMHSA/CBHSQ)
Sent: Monday, November 10, 2014 1:09 PM
To: 'Mark Sharp'
Cc: r-help at r-project.org
Subject: Re: [R] range () does not remove NA's with complete.cases() for dates (dplyr/mutate)

Mark,

Thank you very much for further looking into this issue.  So, the "ugly" solution is better!  Would you like to bring to Hadley's attention that mutate does set the NA value for the new column?

Regards,

Pradip

Pradip K. Muhuri, PhD
SAMHSA/CBHSQ
1 Choke Cherry Road, Room 2-1071
Rockville, MD 20857
Tel: 240-276-1070
Fax: 240-276-1260


-----Original Message-----
From: Mark Sharp [mailto:msharp at TxBiomed.org] 
Sent: Monday, November 10, 2014 12:23 PM
To: Muhuri, Pradip (SAMHSA/CBHSQ)
Cc: r-help at r-project.org
Subject: Re: [R] range () does not remove NA's with complete.cases() for dates (dplyr/mutate)

Pradip,

For some reason mutate is not setting the is.NA value for the new column. Note the output below using your data structures.
[1] NA
[1] NA
[1] "numeric"
[1] "numeric"
Date[1:1], format: NA
Date[1:1], format: NA
[1] "Date"
[1] "Date"
[1] FALSE
[1] "'is.NA' value mismatch: 0 in current 1 in target"
## functioning code
data2$mrjdate[2]
data2$oiddate[2]
mrj <- data2$mrjdate[2]
oid <- data2$oiddate[2]
mode(mrj)
mode(oid)
str(mrj)
str(oid)
class(mrj)
class(oid)
# But note:
identical(mrj, oid)
all.equal(mrj, oid)

## This ugly solution does not have the problem.
+   if (all(is.na(unlist(data1[row, -1])))) {
+     max_d <- NA
+   } else {
+     max_d <- max(unlist(data1[row, -1]), na.rm = TRUE)
+   }
+   max_d}),
+   origin = "1970-01-01")
[1] "2004-11-04" "2009-10-24"
[1] "2005-08-10" "2011-10-05"
[1] "2005-07-07" "2011-10-13"
[1] "2007-11-07" "2011-11-04"
[1] "2006-09-01" "2011-11-04"
Working code below.

data3 <- data1
data3$oiddate <- as.Date(sapply(seq_along(data3$id), function(row) {
  if (all(is.na(unlist(data1[row, -1])))) {
    max_d <- NA
  } else {
    max_d <- max(unlist(data1[row, -1]), na.rm = TRUE)
  }
  max_d}),
  origin = "1970-01-01")

range(data3$mrjdate[complete.cases(data3$mrjdate)])
range(data3$cocdate[complete.cases(data3$cocdate)])
range(data3$inhdate[complete.cases(data3$inhdate)])
range(data3$haldate[complete.cases(data3$haldate)])
range(data3$oiddate[complete.cases(data3$oiddate)])
On Nov 10, 2014, at 10:10 AM, Muhuri, Pradip (SAMHSA/CBHSQ) wrote:

            
NOTICE:  This E-Mail (including attachments) is confidential and may be legally privileged.  It is covered by the Electronic Communications Privacy Act, 18 U.S.C.2510-2521.  If you are not the intended recipient, you are hereby notified that any retention, dissemination, distribution or copying of this communication is strictly prohibited.  Please reply to the sender that you have received this message in error, then delete it.

______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.