[R] generating a vector from clusters of logicals
Achim Zeileis
Achim.Zeileis at wu-wien.ac.at
Thu Sep 8 23:11:12 CEST 2005
On Thu, 08 Sep 2005 23:03:03 +0200 Troels Ring wrote:
> dear friends,
> I have a vector of clusters of TRUE and FALSE like
> c(TRUE,TRUE,TRUE...,FALSE,FALSE,FALSE,....TRUE,TRUE...) and want to
> make that into a vector
> of c(1,1,1,1...2,2,2,2,.....3,3,3,3) increasing the number assigned to
> each cluster as they change.
> How would I do that ?
Does this what you want:
R> set.seed(123)
R> x <- sample(c(TRUE, FALSE), 10, replace = TRUE)
R> x
[1] TRUE FALSE TRUE FALSE FALSE TRUE FALSE FALSE FALSE TRUE
R> c(1, cumsum(abs(diff(x))) + 1)
[1] 1 2 3 4 4 5 6 6 6 7
?
Z
> Best wishes
>
> Troels Ring, Aalborg, Denmark
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide!
> http://www.R-project.org/posting-guide.html
>
More information about the R-help
mailing list