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

后端 未结 2 1722
梦毁少年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 03:47

    dcast() tries to set the value of missing combination by default value.

    you can specify this by fill argument, but if fill=NULL, then the value returned by fun(0-lenght vector) (i.e., summarize(numeric(0)) here) is used as default.

    please see ?dcast

    then, here is a workaround:

     dcast(tab.melt, gene~variable, summarize, fill=NaN)
    
    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题