51

I get the following warning when I use min or max in the dcast function from the reshape2 package. What is it telling me? I can't find anything that explains the warning message and I'm a bit confused about why I get it when I use max but not when I use mean or other aggregate functions.

Warning message:
In .fun(.value[0], ...) : no non-missing arguments to min; returning Inf

Here's a reproducible example:

data(iris)

library(reshape2)

molten.iris <- melt(iris,id.var="Species")
summary(molten.iris)
str(molten.iris)
#------------------------------------------------------------
# Both return warning:
dcast(data=molten.iris,Species~variable,value.var="value",fun.aggregate=min)
dcast(data=molten.iris,Species~variable,value.var="value",fun.aggregate=max)

# Length looks fine though
dcast(data=molten.iris,Species~variable,value.var="value",fun.aggregate=length)

#------------------------------------------------------------
# No warning messages here:
aggregate(value ~ Species + variable, FUN=min, data=molten.iris)
aggregate(value ~ Species + variable, FUN=max, data=molten.iris)
#------------------------------------------------------------
# Or here:
library(plyr)

ddply(molten.iris,c("Species","variable"),function(df){
  data.frame(
    "min"=min(df$value),
    "max"=max(df$value)
    )
})
#------------------------------------------------------------
Tumbledown
  • 1,887
  • 5
  • 21
  • 33
  • 6
    The reason this appears when using `min` or `max` but not `mean` is that `mean` does not throw a warning when applied to a length 0 vector. If you do `dcast(data=molten.iris,Species~variable,value.var="value", function(x) {print(x); min(x)})` you see that the first `x` is a numeric vector of length 0. Since `fill=NULL` in `dcast` by default, then `min` gets applied to the length 0 vector and it produces the warning. The question is why is there this structural pattern that the first element returned is of a length 0 vector... No idea why this happens since all factor combinations seem to exist – konvas Jun 18 '14 at 10:21

1 Answers1

69

You get this warning because the min/max are applied to numeric of length 0 argument.

This reproduces the warning.

min(numeric(0))
[1] Inf
Warning message:
In min(numeric(0)) : no non-missing arguments to min; returning Inf

Note that for mean you don't get the warning :

mean(numeric(0))
[1] NaN

It is just a warning that don't have any effect in the computation. You can suppress it using suppressWarnings:

 suppressWarnings(dcast(data=molten.iris,
                  Species~variable,value.var="value",
                  fun.aggregate=min))

EDIT

Above I am just answering the question: What's the meaning of the warning ? and why we have this min/max and not with mean function. The question why dcast is applying the aggregate function to a vector of length 0, it is just a BUG and you should contact the package maintainer. I think the error comes from plyr::vaggregate function used internally by dcast,

plyr::vaggregate(1:3,1:3,min)
Error in .fun(.value[0], ...) : 
  (converted from warning) no non-missing arguments to min; returning Inf

Specially this line of code:

plyr::vaggregate
function (.value, .group, .fun, ..., .default = NULL, .n = nlevels(.group)) 
{
    ### some lines       
    ....
    ### Here I don't understand the meaning of .value[0]
    ### since vector in R starts from 1 not zeros!!!
    if (is.null(.default)) {
        .default <- .fun(.value[0], ...)
    }
    ## the rest of the function 
    .....
}
Community
  • 1
  • 1
agstudy
  • 119,832
  • 17
  • 199
  • 261
  • Indeed this was my comment as well :) But why does it get applied to a numeric of length 0? The warning can also be avoided by using e.g. `fill = 0` (or any other value) since that will not apply `fill` to the numeric of length 0. – konvas Jun 20 '14 at 15:29
  • @konvas yes. The OP question was `I can't find anything that explains the warning message and I'm a bit confused about why I get it when I use max but not when I use mean` So I am answering this. For `dcast` ugly behavior it is surely a BUG. – agstudy Jun 20 '14 at 16:29
  • Thanks for your edit. I think your original answer (like my comment above) partly answered the OP question but did not go deep enough. This is why I offered the bounty. In my opinion just saying that `min` produces a warning and `mean` doesn't wasn't a complete explanation so what I was looking for was what you wrote in your "EDIT". Thanks again. – konvas Jun 20 '14 at 16:50
  • 8
    Another way to get rid of the warnings is to define `RobustMax <- function(x) {if (length(x)>0) max(x) else -Inf}` and then use that instead of `max`. – Victor Klos Jun 16 '15 at 21:47
  • @VictorKlos Works well and quite straight forward. Thanks. – Apricot Aug 08 '16 at 03:03
  • 2
    What exactly is the reason that the two functions in these cases with only NAs return the slightly weird return value of `+Inf` for `min` and `-Inf` for `max`. – hannes101 Jul 06 '18 at 07:34
  • @hannes101 The minimum and maximum of a numeric empty set are ‘+Inf’ and ‘-Inf’ (in this order!) which ensures _transitivity_, e.g., ‘min(x1, min(x2)) == min(x1, x2)’. For numeric ‘x’ ‘max(x) == -Inf’ and ‘min(x) == +Inf’ whenever ‘length(x) == 0’ (after removing missing values if requested). However, ‘pmax’ and ‘pmin’ return ‘NA’ if all the parallel elements are ‘NA’ even for ‘na.rm = TRUE’. – Matus Goljer Oct 13 '21 at 10:49