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)
归档时间: |
|
查看次数: |
55261 次 |
最近记录: |