[R] counting strings of identical values in a matrix

A M Lavezzi lavezzi at unipa.it
Fri Nov 16 12:11:47 CET 2007


Marc and Gabor
thank you so much.

Also for making me realize how litte I know about R's potential

best,
Mario

ps I actually thought about appending that row of 
zeros while waking up this morning..



At 18.35 15/11/2007, 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
> >
> >
> >
> > On Thu, 2007-11-15 at 17:18 +0100, A M Lavezzi wrote:
> > > Dear Marc
> > > thank you so much!
> > >
> > > One thing: writing xx=[1,2,1,1] is not a typo: I
> > > read it as the count of runs of different length starting from 1.
> > >
> > > In "prova" I have 1 run of length one, 2 runs of
> > > length two, 1 run of length three and 1 run of length four.
> > >
> > > Can I abuse of your time  and ask how to do this?
> > >
> > > Thanks again
> > > Mario
> > >
> > > At 16.48 15/11/2007, you wrote:
> > >
> > > >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
> > >
> > > ===============================
> > > Andrea Mario Lavezzi
> > > Dipartimento di Studi su Politica Diritto e Società
> > > Piazza Bologni 8
> > > 90134 Palermo
> > > tel. ++39 091 6625600
> > > fax ++39 091 6112023
> > > skype: lavezzimario
> > > email: lavezzi at unipa.it
> > > web: http://www.unipa.it/~lavezzi
> > > ===============================
> > >
> >
> > ______________________________________________
> > 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.
> >

===============================
Andrea Mario Lavezzi
Dipartimento di Studi su Politica Diritto e Società
Piazza Bologni 8
90134 Palermo
tel. ++39 091 6625600
fax ++39 091 6112023
skype: lavezzimario
email: lavezzi at unipa.it
web: http://www.unipa.it/~lavezzi
=============================== 



More information about the R-help mailing list