[R] counting strings of identical values in a matrix
Marc Schwartz
marc_schwartz at comcast.net
Thu Nov 15 18:52:23 CET 2007
Thanks Gabor. Nice solution.
Marc
On Thu, 2007-11-15 at 12:35 -0500, Gabor Grothendieck wrote:
> We can append a row of 0's to handle that case:
>
> with(rle(as.vector(rbind(prova, 0))), table(lengths[values == 1]))
>
>
>
> On Nov 15, 2007 11:36 AM, Marc Schwartz <marc_schwartz at comcast.net> wrote:
> > Ah...OK. I misunderstood then. I thought that you wanted the number of
> > runs of 1's in each column.
> >
> > This is actually easier, _if_ there is not an overlap of 1's from the
> > end of one column to the start of the next column:
> >
> > res <- rle(as.vector(prova))
> >
> > > res
> > Run Length Encoding
> > lengths: int [1:11] 2 2 2 2 3 3 5 4 2 1 ...
> > values : int [1:11] 3 1 3 1 3 1 3 1 3 1 ...
> >
> > > table(res$lengths[res$values == 1])
> >
> > 1 2 3 4
> > 1 2 1 1
> >
> >
> > HTH,
> >
> > Marc
> >
<snip>
More information about the R-help
mailing list