[R] counting run lengths
Richard.Cotton at hsl.gov.uk
Richard.Cotton at hsl.gov.uk
Mon Oct 27 15:24:28 CET 2008
> >> It works, but the for (i in ...) loop slows down the simulation a
lot.
> >>
> >> Any suggestion on how to avoid this loop? (or in general, to speed up
> >> this part of the simulation)
> Actually, I have not specified the following: i want to consider only
> the "most recent" sequence of zeros, that is the last part of the time
> series.
>
> That is, If I have:
>
> [,1] [,2] [,3] [,4]
> [1,] 0 1 0 1
> [2,] 1 1 1 1
> [3,] 1 1 0 1
> [4,] 1 1 0 1
> [5,] 1 1 0 1
> [6,] 0 1 0 1
> [7,] 0 1 0 1
> [8,] 0 1 0 1
> [9,] 0 1 0 1
>
> I want to store the values (4 0 7 0) in unSpells. That is the values
> that represent the "last" sequence of zeros (that is I want to ignore
> other possible zeros like the ones I have inserted above).
This runs pretty quickly:
unSpells <- nrow(Atr) - apply(Atr,2,function(x) max(which(x==1)))
#c(4,0,7,0)
Regards,
Richie.
Mathematical Sciences Unit
HSL
------------------------------------------------------------------------
ATTENTION:
This message contains privileged and confidential inform...{{dropped:20}}
More information about the R-help
mailing list