[R] applying a function over a matrix 2N x M to produce a matrix N x M

Christine SINOQUET christine.sinoquet at univ-nantes.fr
Fri Apr 22 18:13:04 CEST 2011


Hello,

mat1 only consists of 0s and 1s:
0 0 1 0 0 0
1 1 0 1 1 0
1 1 1 0 1 0
0 1 1 0 0 1
1 0 0 1 0 0
0 1 0 1 0 1

N = 3
M = 6

I would like to "compress" mat1 every two rows, applying summation over 
the two rows (per column), at each step, to yield:

mat2
1 1 1 1 1 0
1 2 2 0 1 1
1 1 0 2 0 1

Then, in mat2, I shall have to count the number of 0s, 1s and 2s, per 
column, which is my final aim.

I am aware of possibilities such as

counts <- sapply(mat2,2,fun1)

but I do not know how to write fun1.


Besides, it was perhaps not necessary to waste memory producing the 
temporary matrix mat2.

Can somebody help ?

I thank you in advance for your answer.

Best regards,

Christine Sinoquet



More information about the R-help mailing list