在reshape2中使用min或max时,不会出现非遗漏的参数警告 [英] No non-missing arguments warning when using min or max in reshape2

查看:449
本文介绍了在reshape2中使用min或max时,不会出现非遗漏的参数警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在reshape2软件包的dcast函数中使用min或max时,收到以下警告。它告诉我什么?我找不到能解释该警告消息的任何内容,我对为什么在使用max时得到警告而不是在使用均值或其他聚合函数时得到警告感到困惑。

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.


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

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


推荐答案

您会收到此警告,因为最小值/最大值应用于长度为0的参数的数字。

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

这会重现警告。

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

这只是一个警告,它对计算没有任何影响。您可以使用 suppressWarnings 抑制它:

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



编辑



上面我只是在回答一个问题:警告的含义是什么?以及为什么我们有这个最小值/最大值而不是均值函数。为什么 dcast 将聚合函数应用于长度为0的向量的问题,它只是一个BUG,您应该与软件包维护者联系。我认为错误来自 dcast

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

特别是以下代码行:

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

这篇关于在reshape2中使用min或max时,不会出现非遗漏的参数警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆