且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

R reshape2中的cast()调用的自定义聚合函数出错

更新时间:2021-09-24 20:22:00

短回答:提供填充值如下
acast(tab.melt,gene〜variable,summarize,fill = 0)

Short answer: provide a value for fill as follows acast(tab.melt, gene~variable, summarize, fill=0)

长回答:
在vaggregate函数(dcast调用cast,它调用vaggregate,它调用vapply)传递给vapply之前,你的函数看起来如下所示:

Long answer: It appears your function gets wrapped as follows, before being passed to vapply in the vaggregate function (dcast calls cast which calls vaggregate which calls vapply):

fun <- function(i) {
    if (length(i) == 0) 
        return(.default)
    .fun(.value[i], ...)
}

要找出.default应该是什么,

To find out what .default should be, this code is executed

if (is.null(.default)) {
    .default <- .fun(.value[0])
}

ie .value [0]传递给函数。当x是数字(0)时,min(x)或max(x)返回Inf或-Inf。然而,max(x)/ min(x)返回具有类逻辑的NaN。所以当执行vapply时

i.e. .value[0] is passed to the function. min(x) or max(x) returns Inf or -Inf on when x is numeric(0). However, max(x)/min(x) returns NaN which has class logical. So when vapply is executed

vapply(indices, fun, .default)

默认值为逻辑类型(用作vapply的模板),函数在开始返回双精度时失败。

with the default value being is of class logical (used as template by vapply), the function fails when starting to return doubles.