[R] counting strings of identical values in a matrix

Marc Schwartz marc_schwartz at comcast.net
Thu Nov 15 16:48:55 CET 2007


On Thu, 2007-11-15 at 15:51 +0100, A M Lavezzi wrote:
> Hello
> 
> I have this problem. I have a large matrix of this sort:
> 
>  > prova
>       [,1] [,2] [,3] [,4]
> [1,]    3    3    3    3
> [2,]    3    3    3    1
> [3,]    1    3    3    3
> [4,]    1    1    1    3
> [5,]    3    1    1    3
> [6,]    3    1    1    3
> [7,]    1    3    1    3
> [8,]    1    3    3    3
> 
> What I want to do is to count the number of 
> sequences of ones and stack the results in a 
> vector, which I will subsequently use to build an istogram (or a density)
> 
> I mean: in the matrix "prova" I have two 
> sequences of length two in column 1, one sequence 
> of length three in column 2, one sequence  of 
> length four in column 3 and one sequence of 
> length one in column 4. (I know I can actually 
> turn the matrix into a vector by using rep(prova))
> 
> I would like to get to a vector such as : xx = [1,2,1,1]

I presume a typo above and that it should be:  

  xx = [2,1,1,1]

?

If so:

> unlist(lapply(apply(prova, 2, rle), 
                function(x) length(x$lengths[x$values == 1])))
[1] 2 1 1 1


See ?rle to get the basics of identifying runs of values.

HTH,

Marc Schwartz



More information about the R-help mailing list