Hello! If I have a vector vec <- c(FALSE, FALSE, TRUE, FALSE, TRUE, TRUE, FALSE FALSE) I can I create the following order of numbers based on vector vec: 1, 2, 2, 3, 3, 3, 4, 5 Whenever there is a FALSE I increase the number (starting with 1). Whenever there is a TRUE I set the same number as the previous FALSE has been assigned to. I would be happy for any input Cheers, Syrvn -- View this message in context: http://r.789695.n4.nabble.com/Create-order-of-numbers-based-on-a-given-vector-tp3901158p3901158.html Sent from the R help mailing list archive at Nabble.com.
I think you can use the cumsum function. If you think of your falses to 1 and your trues to 0 then you're just sequentially adding the numbers in the vector. x = c(FALSE, FALSE, TRUE, FALSE, TRUE, TRUE, FALSE, FALSE) y = rep(1,length(x))*(1-x) cumsum(y) Hope that helps, Sam On Thu, Oct 13, 2011 at 8:15 AM, syrvn <mentor_ at gmx.net> wrote:> Hello! > > If I have a vector vec <- c(FALSE, FALSE, TRUE, FALSE, TRUE, TRUE, FALSE > FALSE) > > I can I create the following order of numbers based on vector vec: > > 1, 2, 2, 3, 3, 3, 4, 5 > > Whenever there is a FALSE I increase the number (starting with 1). > Whenever there is a TRUE I set the same number as the previous FALSE has > been assigned to. > > > I would be happy for any input > > Cheers, > Syrvn > > -- > View this message in context: http://r.789695.n4.nabble.com/Create-order-of-numbers-based-on-a-given-vector-tp3901158p3901158.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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. >
Dimitris Rizopoulos
2011-Oct-13 11:25 UTC
[R] Create order of numbers based on a given vector
try this: vec <- c(FALSE, FALSE, TRUE, FALSE, TRUE, TRUE, FALSE, FALSE) cumsum(!vec) I hope it helps. Best, Dimitris On 10/13/2011 1:15 PM, syrvn wrote:> Hello! > > If I have a vector vec<- c(FALSE, FALSE, TRUE, FALSE, TRUE, TRUE, FALSE > FALSE) > > I can I create the following order of numbers based on vector vec: > > 1, 2, 2, 3, 3, 3, 4, 5 > > Whenever there is a FALSE I increase the number (starting with 1). > Whenever there is a TRUE I set the same number as the previous FALSE has > been assigned to. > > > I would be happy for any input > > Cheers, > Syrvn > > -- > View this message in context: http://r.789695.n4.nabble.com/Create-order-of-numbers-based-on-a-given-vector-tp3901158p3901158.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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. >-- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus University Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014 Web: http://www.erasmusmc.nl/biostatistiek/
I don't have access to R so I can't test my example but I think this will work. vec ( as defined by you) # flip the false and the trues vec1<-ifelse(vec==FALSE,TRUE,FALSE) ans<-cumsum(vec) Regards, Ashim On Thu, Oct 13, 2011 at 4:45 PM, syrvn <mentor_ at gmx.net> wrote:> Hello! > > If I have a vector vec <- c(FALSE, FALSE, TRUE, FALSE, TRUE, TRUE, FALSE > FALSE) > > I can I create the following order of numbers based on vector vec: > > 1, 2, 2, 3, 3, 3, 4, 5 > > Whenever there is a FALSE I increase the number (starting with 1). > Whenever there is a TRUE I set the same number as the previous FALSE has > been assigned to. > > > I would be happy for any input > > Cheers, > Syrvn > > -- > View this message in context: http://r.789695.n4.nabble.com/Create-order-of-numbers-based-on-a-given-vector-tp3901158p3901158.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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. >