Applying functions to partitions
Stavros Macrakis <macrakis at alum.mit.edu> writes:
Assuming your matrix is:
mm <- matrix(runif(6*6),6,6)
And your blocks are defined by integers or factors:
cfact <- c(1,1,1,2,3,3)
rfact <- c(1,1,1,2,2,3)
Then the following should do the trick:
matrix(tapply(mm, outer(rfact,cfact,paste), mean),
length(unique(rfact)))
or the variant idx <- outer(rfact, (cfact - 1) * max(rfact), "+") matrix(tapply(m, idx, mean), max(rfact)) The assumption is that cfact, rfact are integer valued with max(rfact) <= nrow(m), max(cfact) <= ncol(m). I think Stavros' solution will run in to trouble when there are more than 9 row blocks, and '10 1' sorts before '2 1', for instance. Martin
The 'outer' calculates a joint factor for each element of the matrix; the
'tapply' treats the matrix as a vector, grouping by factor and calculating
means; the 'matrix' rearranges them as a matrix corresponding to the
original block structure.
Is that what you had in mind?
-s
On Mon, Feb 16, 2009 at 12:43 PM, Titus von der Malsburg <malsburg at gmail.com
wrote:
Hi list! I have a large matrix which I'd like to partition into blocks
and for each block I'd like to compute the mean. Following a example
where each letter marks a block of the partition:
a a a d g g
a a a d g g
a a a d g g
b b b e h h
b b b e h h
c c c f i i
I'm only interested in the resulting matrix of means. How can this be
done efficiently?
Thanks! Titus
______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[[alternative HTML version deleted]]
______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Martin Morgan Computational Biology / Fred Hutchinson Cancer Research Center 1100 Fairview Ave. N. PO Box 19024 Seattle, WA 98109 Location: Arnold Building M2 B169 Phone: (206) 667-2793