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

警告消息:
在.乐趣(.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)
    )
})
#------------------------------------------------------------

推荐答案

出现此警告是因为最小/最大值应用于长度为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来 suppress 它:

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

编辑

以上我只是回答一个问题:警告的意义是什么?为什么我们有这个最小/最大值,而不是均值函数.问题是为什么dcast将聚合函数应用于长度为0的向量,这只是一个BUG,您应该联系软件包维护人员.我认为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 
    .....
}

R相关问答推荐

如何将log 2刻度上的数字转换为自然log

使用case_when和Mutate搜索多个列以寻找条件

通过Plotly绘制线串几何形状的3D图

基于R中的GPS点用方向箭头替换点

编码变量a、b、c以匹配来自另一个数据点的变量x

使用tidyverse / Mutate的存款账户余额

从gtsummary包中使用tBL_strata()和tBL_summary()时删除变量标签

r—绘制相交曲线

如何在R中对深度嵌套的tibbles中的非空连续行求和?

将包含卷的底部25%的组拆分为2行

在R gggplot2中是否有一种方法将绘图轴转换成连续的 colored颜色 尺度?

使用带有OR条件的grepl过滤字符串

在数据帧列表上绘制GGPUP

从多面条形图中删除可变部分

R中的类别比较

如何删除R中除数字元素以外的所有元素

将统计检验添加到GGPUBR中的盒图,在R

如何提取R中其他字符串和数字之间的字符串?

有没有办法将不等长的列表转换为R中的数据帧

重写时间间隔模糊连接以减少内存消耗