我想使用R将带有非唯一rownames的表中的数值数据汇总到具有唯一行名的结果表中,并使用自定义函数汇总值。汇总逻辑是:如果最大值与最小值的比值为< 1.5,则使用值的均值,否则使用中值。由于表格非常大,我正尝试使用reshape2包中的melt()和cast()函数。R中的cast()调用的自定义聚合函数的错误reshape2
# 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)
上面的代码最后一行产生了错误通知。
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
我在做什么错?请注意,如果汇总函数仅返回min()或max(),则不会出现错误,尽管存在有关'没有非缺少参数'的警告消息。谢谢你的任何建议。
(实际的表我想使用的是200x10000一个。)
感谢您关注此问题并提供帮助。 – user594694 2011-01-30 08:14:19