[R] Applying functions to partitions

David Winsemius dwinsemius at comcast.net
Mon Feb 16 21:39:49 CET 2009


Its not clear that the object returned from such an operation would be  
a matrix, but if things remain very regular then perhapos you will  
succeed with this:

 > markmtx <- matrix(scan(textConnection("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"), what="character"), nrow=6)
Read 36 items
 > nummtx <-matrix(rnorm(36),nrow=6)
 > nummtx
             [,1]       [,2]        [,3]         [,4]       [,5]        
[,6]
[1,] -1.49492952 -0.1000962 -0.54546587 -0.536216056  0.1065169  
-1.3368842
[2,] -0.64393278  0.3343573  0.76247880  0.282666215  0.2236401   
0.8210809
[3,]  1.42879752 -1.3246770  0.06403316 -0.002843621 -0.2990221  
-0.4885461
[4,] -0.38740975  0.7800235  0.12819144  0.206188106  0.8481351   
0.2572268
[5,] -0.07082702 -0.7870970  0.60560030 -1.381615740  1.4935228   
0.1165892
[6,] -0.06916424 -0.5869168  0.39492984  0.016430970 -0.6531722  
-0.1194990

 > tapply(nummtx, markmtx, FUN=mean)
            a            b            c            d             
e            f            g
-0.168826070 -0.037543099 -0.334783145  0.173601720  0.527161589   
0.257226798 -0.085579151
            h            i
-0.131208561 -0.001454904

 > matrix(tapply(nummtx, markmtx, FUN=mean), nrow=3)
            [,1]      [,2]         [,3]
[1,] -0.1688261 0.1736017 -0.085579151
[2,] -0.0375431 0.5271616 -0.131208561
[3,] -0.3347831 0.2572268 -0.001454904

-- 
David Winsemius

On Feb 16, 2009, at 12:43 PM, Titus von der Malsburg 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.




More information about the R-help mailing list