Error with custom aggregate function for a cast() call in R reshape2

后端 未结 2 1735
梦毁少年i
梦毁少年i 2021-01-05 02:56

I want to use R to summarize numerical data in a table with non-unique rownames to a result table with unique row-names with values summarized using a custom function. The s

2条回答
  •  爱一瞬间的悲伤
    2021-01-05 04:04

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

    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], ...)
    }
    

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

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

    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)
    

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

提交回复
热议问题