在reshape2中使用min或max时,没有非缺失参数警告

Tum*_*own 33 r aggregate-functions reshape2

当我在reshape2包中的dcast函数中使用min或max时,我收到以下警告.它告诉我什么?我找不到任何解释警告信息的东西,我有点困惑,为什么我在使用max时得到它而不是当我使用mean或其他聚合函数时.

警告消息:
在.fun(.value [0],...)中:min没有非缺失参数; 返回Inf

这是一个可重复的例子:

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)
    )
})
#------------------------------------------------------------
Run Code Online (Sandbox Code Playgroud)

ags*_*udy 44

您收到此警告是因为min/max应用于长度为0的参数的数字.

这再现了警告.

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

请注意,mean您没有收到警告:

mean(numeric(0))
[1] NaN
Run Code Online (Sandbox Code Playgroud)

这只是一个警告,对计算没有任何影响.您可以使用suppressWarnings以下方法禁止它

 suppressWarnings(dcast(data=molten.iris,
                  Species~variable,value.var="value",
                  fun.aggregate=min))
Run Code Online (Sandbox Code Playgroud)

编辑

以上我只是回答这个问题:警告的含义是什么?为什么我们有这个最小/最大而不是平均功能.问题为什么dcast将聚合函数应用于长度为0的向量,它只是一个BUG,你应该联系包维护者.我认为错误来自plyr::vaggregate内部使用的函数dcast,

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

特别是这行代码:

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 
    .....
}
Run Code Online (Sandbox Code Playgroud)

  • 摆脱警告的另一种方法是定义`RobustMax < - function(x){if(length(x)> 0)max(x)else -Inf}`然后使用它而不是`max`. (3认同)
  • 在这些情况下,只有 NA 的两个函数返回稍微奇怪的返回值“min”的“+Inf”和“max”的“-Inf”,到底是什么原因呢? (2认同)