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

51

当我在reshape2包中使用dcast函数的min或max时,我会收到以下警告信息。这是在告诉我什么?我找不到任何解释警告信息的内容,我有点困惑,不知道为什么在使用max时会出现这个警告,而在使用平均值或其他聚合函数时却没有。

警告信息:
在 .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)
    )
})
#------------------------------------------------------------

6
使用minmax会出现该警告,但使用mean不会,原因在于mean应用于长度为0的向量时不会抛出警告。如果执行dcast(data=molten.iris,Species~variable,value.var="value", function(x) {print(x); min(x)}),你将看到第一个x是长度为0的数字向量。由于在默认情况下 dcastfill=NULL,因此min被应用于长度为0的向量并产生了警告。问题是为什么存在这种结构模式,即返回的第一个元素是长度为0的向量...不知道为什么会发生这种情况,因为所有因子组合似乎都存在。 - konvas
1个回答

69
您会收到此警告是因为min/max应用于长度为0的数字参数。这会重现警告。
min(numeric(0))
[1] Inf
Warning message:
In min(numeric(0)) : no non-missing arguments to min; returning Inf

请注意,在使用 mean 函数时,您将不会收到警告信息。
mean(numeric(0))
[1] NaN

这只是一个警告,不会对计算产生任何影响。您可以使用suppressWarnings来抑制它:

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

编辑

以上我只是回答问题:这个警告的含义是什么?为什么我们使用min/max而不是mean函数。关于为什么dcast将聚合函数应用于长度为0的向量,这只是一个错误,您应该联系软件包维护人员。我认为错误来自dcast内部使用的plyr::vaggregate函数。

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

特别是这行代码:
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 
    .....
}

确实,这也是我的评论 :) 但为什么它会应用到长度为0的数字?也可以通过使用例如 fill=0(或任何其他值)来避免警告,因为那样不会将 fill 应用于长度为0的数字。 - konvas
8
消除警告的另一种方式是定义RobustMax <- function(x) {if (length(x)>0) max(x) else -Inf},然后使用它来替代max - Victor Klos
@VictorKlos 做得很好,而且非常直接明了。谢谢。 - Apricot
2
在这些情况下,仅包含NA的两个函数返回min的略微奇怪的返回值为+Inf,而max的返回值为-Inf,究竟是什么原因呢? - hannes101
@hannes101 数值空集的最小值和最大值分别为“+Inf”和“-Inf”(按此顺序!),这确保了传递性,例如,“min(x1,min(x2))== min(x1,x2)”。对于数值“x”,当“length(x)== 0”(如果请求删除缺失值)时,“max(x)== -Inf”和“min(x)== +Inf”。但是,“pmax”和“pmin”即使对于“na.rm = TRUE”,如果所有并行元素都是“NA”,也会返回“NA”。 - Matus Goljer
显示剩余2条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接