I want to use R to sum the numeric data in a table with unique growth names in the result table with unique row names with values ββsummed using a custom function. Summing logic: use the average value if the ratio of the maximum to the minimum value is <1.5, otherwise use the median. Since the table is very large, I am trying to use the melt () and cast () functions in the reshape2 package.
# example table with non-unique row-names
tab <- data.frame (gene = rep (letters [1: 3], each = 3), s1 = runif (9), s2 = runif (9))
# melt
tab.melt <- melt (tab, id = 1)
# function to summarize with logic: mean if max / min <1.5, else median
summarize <- function (x) {ifelse (max (x) / min (x) <1.5, mean (x), median (x))}
# cast with summarized values
dcast (tab.melt, gene ~ variable, summarize) An error message appears in the last line of code.
Error in vapply (indices, fun, .default):
values ββmust be type 'logical',
but FUN (X [[1]]) result is type 'double'
In addition: Warning messages:
1: In max (x): no non-missing arguments to max; returning -Inf
2: In min (x): no non-missing arguments to min; returning inf
What am I doing wrong? Please note that if the summation function was to simply return min () or max (), there was no error, although there is a warning message about "missing arguments". Thanks for any suggestion.
(The actual table I want to work with is 200x10000).
casting r aggregate reshape2 reshape
user594694
source share