Jorge Molinos
2012-Mar-08 14:35 UTC
[R] Calculating length of consecutive sequences within a vector
Hi all, I have a nx1 logical array of zeros and ones and I want to calculate the individual lengths of all 1-consecutive sequences contained in it. Is there an easy quick way to do this in R? So, if I have a vector such as 111001101000011111110 I would like to get (1) 3, (2) 2, (3) 1, (4) 7 Any help would be appreciated! thanks! Jorge [[alternative HTML version deleted]]
R. Michael Weylandt
2012-Mar-08 16:29 UTC
[R] Calculating length of consecutive sequences within a vector
rle should get you started. Michael On Thu, Mar 8, 2012 at 9:35 AM, Jorge Molinos <JGARCIAM at tcd.ie> wrote:> Hi all, > > I have a nx1 logical array of zeros and ones and I want to calculate the individual lengths of all 1-consecutive sequences contained in it. Is there an easy quick way to do this in R? So, if I have a vector such as > > 111001101000011111110 > > I would like to get (1) 3, (2) 2, (3) 1, (4) 7 > > Any help would be appreciated! thanks! > > Jorge > > ? ? ? ?[[alternative HTML version deleted]] > > ______________________________________________ > 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.
Sarah Goslee
2012-Mar-08 16:35 UTC
[R] Calculating length of consecutive sequences within a vector
?rle If your "vector" is as shown, you'll need to split it into its component elements first, possibly with strsplit(). If you use dput() or some other means of providing a reproducible example, someone might be inclined to offer you actual code. Sarah On Thu, Mar 8, 2012 at 9:35 AM, Jorge Molinos <JGARCIAM at tcd.ie> wrote:> Hi all, > > I have a nx1 logical array of zeros and ones and I want to calculate the individual lengths of all 1-consecutive sequences contained in it. Is there an easy quick way to do this in R? So, if I have a vector such as > > 111001101000011111110 > > I would like to get (1) 3, (2) 2, (3) 1, (4) 7 > > Any help would be appreciated! thanks! > > Jorge >-- Sarah Goslee http://www.functionaldiversity.org
Jorge Molinos
2012-Mar-09 11:28 UTC
[R] Calculating length of consecutive sequences within a vector
Thanks, rle is what I was looking for. Jorge ________________________________________ From: R. Michael Weylandt [michael.weylandt at gmail.com] Sent: 08 March 2012 16:29 To: Jorge Molinos Cc: r-help at R-project.org Subject: Re: [R] Calculating length of consecutive sequences within a vector rle should get you started. Michael On Thu, Mar 8, 2012 at 9:35 AM, Jorge Molinos <JGARCIAM at tcd.ie> wrote:> Hi all, > > I have a nx1 logical array of zeros and ones and I want to calculate the individual lengths of all 1-consecutive sequences contained in it. Is there an easy quick way to do this in R? So, if I have a vector such as > > 111001101000011111110 > > I would like to get (1) 3, (2) 2, (3) 1, (4) 7 > > Any help would be appreciated! thanks! > > Jorge > > [[alternative HTML version deleted]] > > ______________________________________________ > 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.