I have a binary vector and I want to find all "regions" of that vector that are runs of TRUE (or FALSE). > a <- rnorm(10) > b <- a<0.5 > b [1] TRUE TRUE TRUE FALSE TRUE FALSE TRUE TRUE TRUE TRUE My function would return something like a list: region[[1]] 1,3 region[[2]] 5,5 region[[3]] 7,10 Any ideas besides looping and setting start and ends directly? Thanks, Sean
Untested: c(TRUE, b[-1] != b[-length(b)]) gives you the (logical) indexes of the beginnings of the runs c(b[-1] != b[-length(b)], TRUE) gives you the (logical) indexes of the ends of the runs> -----Original Message----- > From: r-help-bounces at stat.math.ethz.ch > [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Sean Davis > Sent: Thursday, January 27, 2005 2:14 PM > To: r-help > Subject: [R] Finding "runs" of TRUE in binary vector > > I have a binary vector and I want to find all "regions" of > that vector that are runs of TRUE (or FALSE). > > > a <- rnorm(10) > > b <- a<0.5 > > b > [1] TRUE TRUE TRUE FALSE TRUE FALSE TRUE TRUE TRUE TRUE > > My function would return something like a list: > region[[1]] 1,3 > region[[2]] 5,5 > region[[3]] 7,10 > > Any ideas besides looping and setting start and ends directly? > > Thanks, > Sean > > ______________________________________________ > 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 >
Thanks Patrick, Albyn, and Vadim. rle() does what I want and, Vadim, your method gives the same results in a different form. I appreciate the help! Sean On Jan 27, 2005, at 5:29 PM, Vadim Ogranovich wrote:> Untested: > > c(TRUE, b[-1] != b[-length(b)]) gives you the (logical) indexes of the > beginnings of the runs > c(b[-1] != b[-length(b)], TRUE) gives you the (logical) indexes of the > ends of the runs > >> -----Original Message----- >> From: r-help-bounces at stat.math.ethz.ch >> [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Sean Davis >> Sent: Thursday, January 27, 2005 2:14 PM >> To: r-help >> Subject: [R] Finding "runs" of TRUE in binary vector >> >> I have a binary vector and I want to find all "regions" of >> that vector that are runs of TRUE (or FALSE). >> >>> a <- rnorm(10) >>> b <- a<0.5 >>> b >> [1] TRUE TRUE TRUE FALSE TRUE FALSE TRUE TRUE TRUE TRUE >> >> My function would return something like a list: >> region[[1]] 1,3 >> region[[2]] 5,5 >> region[[3]] 7,10 >> >> Any ideas besides looping and setting start and ends directly? >> >> Thanks, >> Sean >> >> ______________________________________________ >> 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 >>
Sean Davis <sdavis2 at mail.nih.gov> writes:> I have a binary vector and I want to find all "regions" of that vector > that are runs of TRUE (or FALSE). > > > a <- rnorm(10) > > b <- a<0.5 > > b > [1] TRUE TRUE TRUE FALSE TRUE FALSE TRUE TRUE TRUE TRUE > > My function would return something like a list: > region[[1]] 1,3 > region[[2]] 5,5 > region[[3]] 7,10 > > Any ideas besides looping and setting start and ends directly?You could base it on> rle(b)Run Length Encoding lengths: int [1:5] 1 1 2 4 2 values : logi [1:5] TRUE FALSE TRUE FALSE TRUE> b[1] TRUE FALSE TRUE TRUE FALSE FALSE FALSE FALSE TRUE TRUE (Notice that my b differs from yours) then you might proceed with> end <- cumsum(rle(b)$lengths) > start <- rev(length(b) + 1 - cumsum(rev(rle(b)$lengths))) > # or: start <- c(1, end[-length(end)] + 1) > cbind(start,end)[rle(b)$values,]start end [1,] 1 1 [2,] 3 4 [3,] 9 10 -- O__ ---- Peter Dalgaard Blegdamsvej 3 c/ /'_ --- Dept. of Biostatistics 2200 Cph. N (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
james.holtman@convergys.com
2005-Jan-28 02:28 UTC
[R] Finding "runs" of TRUE in binary vector
use 'rle';> a <- rnorm(20) > b <- a < .5 > b[1] FALSE TRUE TRUE TRUE TRUE TRUE TRUE TRUE FALSE FALSE TRUE TRUE [13] FALSE FALSE TRUE TRUE TRUE FALSE TRUE FALSE> rle(b)Run Length Encoding lengths: int [1:9] 1 7 2 2 2 3 1 1 1 values : logi [1:9] FALSE TRUE FALSE TRUE FALSE TRUE FALSE TRUE FALSE>__________________________________________________________ James Holtman "What is the problem you are trying to solve?" Executive Technical Consultant -- Office of Technology, Convergys james.holtman at convergys.com +1 (513) 723-2929 Sean Davis <sdavis2 at mail.nih.gov To: r-help <r-help at stat.math.ethz.ch> > cc: Sent by: Subject: [R] Finding "runs" of TRUE in binary vector r-help-bounces at stat.m ath.ethz.ch 01/27/2005 17:13 I have a binary vector and I want to find all "regions" of that vector that are runs of TRUE (or FALSE). > a <- rnorm(10) > b <- a<0.5 > b [1] TRUE TRUE TRUE FALSE TRUE FALSE TRUE TRUE TRUE TRUE My function would return something like a list: region[[1]] 1,3 region[[2]] 5,5 region[[3]] 7,10 Any ideas besides looping and setting start and ends directly? Thanks, Sean ______________________________________________ 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
Kjetil Brinchmann Halvorsen
2005-Jan-28 10:44 UTC
[R] Finding "runs" of TRUE in binary vector
Sean Davis wrote:> I have a binary vector and I want to find all "regions" of that vector > that are runs of TRUE (or FALSE). > > > a <- rnorm(10) > > b <- a<0.5 > > b > [1] TRUE TRUE TRUE FALSE TRUE FALSE TRUE TRUE TRUE TRUE > > My function would return something like a list: > region[[1]] 1,3 > region[[2]] 5,5 > region[[3]] 7,10 > > Any ideas besides looping and setting start and ends directly? >?rle> Thanks, > Sean > > ______________________________________________ > 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 > > >-- Kjetil Halvorsen. Peace is the most effective weapon of mass construction. -- Mahdi Elmandjra -- No virus found in this outgoing message. Checked by AVG Anti-Virus.