[R] Applying functions to partitions
Charles C. Berry
cberry at tajo.ucsd.edu
Mon Feb 16 21:02:45 CET 2009
On Mon, 16 Feb 2009, Titus von der Malsburg wrote:
> On Mon, Feb 16, 2009 at 01:45:52PM -0500, Stavros Macrakis wrote:
>> How are the blocks defined? As a priori index ranges? By factors? By
>> some property of i,j? Or...?
>
> Ok, I should have been more specific.
>
> The blocks are defined by factors. There's a factor for the columns and
> a factor for the rows. In the example below the column factor would be
> c(1,1,1,2,3,3) and the row factor c(1,1,1,2,2,3). In the particular
> case I'm working on the matrix is square and symmetric and there's only
> one factor for both.
If the number of factor levels is manageable and if as.numeric(
your.factor ) orders the blocks properly something like this
mat <- model.matrix( ~ 0 + your.factor )
tab <- table( your.factor )
means <- (t(mat) %*% your.block.matrix %*% mat) / outer( tab, tab )
Alternatively, use
means <-
sapply(split.data.frame(your.block.matrix,your.factor),function(x)
sapply(split(colMeans(x),your.factor),mean))
HTH,
Chuck
>
> I can figure out ways to subset the matrix, similar to what Jorge
> proposed, but I'm looking for a way to get the means more or less at
> once because the matrix is pretty large and doing it block-wise is too
> slow.
>
> Thanks again!
>
> Titus
>
>
>> On 2/16/09, 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.
>>>
>>
>
> ______________________________________________
> 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.
>
Charles C. Berry (858) 534-2098
Dept of Family/Preventive Medicine
E mailto:cberry at tajo.ucsd.edu UC San Diego
http://famprevmed.ucsd.edu/faculty/cberry/ La Jolla, San Diego 92093-0901
More information about the R-help
mailing list