[Bioc-devel] vectorize default dist2 function in genefilter
James F. Reid
reidjf at gmail.com
Tue Mar 12 16:43:16 CET 2013
Dear bioc-devel,
the dist2 function in genefilter defined as:
dist2 <- function (x, fun = function(a, b) mean(abs(a - b), na.rm =
TRUE), diagonal = 0) {
if (!(is.numeric(diagonal) && (length(diagonal) == 1L)))
stop("'diagonal' must be a numeric scalar.")
res = matrix(diagonal, ncol = ncol(x), nrow = ncol(x))
colnames(res) = rownames(res) = colnames(x)
if (ncol(x) >= 2) {
for (j in 2:ncol(x)) for (i in 1:(j - 1)) res[i, j] = res[j,
i] = fun(x[, i], x[, j])
}
return(res)
}
could have it's default function vectorized as:
res <- apply(x, 2, function(i) colMeans(abs(x - i), na.rm=TRUE))
to improve performance for example in the ArrayQualityMetrics package.
Best.
James.
More information about the Bioc-devel
mailing list