[R] counting run lengths
Mario Lavezzi
lavezzi at unipa.it
Mon Oct 27 16:54:03 CET 2008
Dear Richard, Martin, Dimitris and Domenico
thank you very much for your help.
I must say that the fastest procedure appears to be the one suggested by
Richard
> This runs pretty quickly:
> unSpells <- nrow(Atr) - apply(Atr,2,function(x) max(which(x==1)))
> #c(4,0,7,0)
>
If I may abuse of your kindness (and maybe of other R users), let me
specify the following: I need to simulate this process for a very high
number of periods (hundreds of thousands). The part of the code I am
using right now, which follows Richard's suggestion, is:
unSpells <- (tt+1) - apply(Atr[1:(tt+1),],2,function(x) max(which(x==1)))
unSpells[unSpells==0]=1
where "tt" indicates the period reached by the simulation (I also need
to turn the zeros in ones in "unSpells").
So, the function applies to a portion of the Atr matrix with a number of
rows increasing with tt.
In this case, however, the "max" operation applies to the whole column
(whose length, clearly, increases with tt as well), while it would be
optimal to have this operation "forgetting" the part of the column
examined in the previous step.
If you have any suggestion, I would be very grateful.
Thanks to all!
Mario
--
Andrea Mario Lavezzi
Dipartimento di Studi su Politica, Diritto e Società
Università di Palermo
Piazza Bologni 8
90134 Palermo, Italy
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