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
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)
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.