在reshape2中使用最小值或最大值时,不存在无缺失参数警告

时间:2021-11-10 22:49:26

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.

当我在reshape2包的dcast函数中使用min或max时,会得到以下警告。它告诉我什么?我找不到任何能解释警告信息的东西,我有点搞不清为什么我用max而不是用均值或其他集合函数。

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

警告消息:.fun(。值[0],…):对min没有无缺失的参数;恢复正

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)
    )
})
#------------------------------------------------------------

1 个解决方案

#1


34  

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

您会得到这个警告,因为最小/最大值应用于长度为0的数值参数。

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,

上面我只是在回答这个问题:这个警告的意义是什么?为什么我们有最小/最大值而不是平均值函数。为什么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

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 
    .....
}

#1


34  

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

您会得到这个警告,因为最小/最大值应用于长度为0的数值参数。

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,

上面我只是在回答这个问题:这个警告的意义是什么?为什么我们有最小/最大值而不是平均值函数。为什么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

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 
    .....
}