Hi, Here is one way using rle():
> x = sample(0:1,42,TRUE) > rle(x) Run Length Encoding lengths: int [1:16] 2 2 2 3 1 1 2 1 3 3 ... values : int [1:16] 1 0 1 0 1 0 1 0 1 0 ... > x [1] 1 1 0 0 1 1 0 0 0 1 0 1 1 0 1 1 1 0 0 0 1 1 1 1 1 1 1 1 1 0 1 0 0 0 1 1 1 1 1 1 1 0 > r <- rle(x) > rle(x) Run Length Encoding lengths: int [1:16] 2 2 2 3 1 1 2 1 3 3 ... values : int [1:16] 1 0 1 0 1 0 1 0 1 0 ... > with(r, lengths * values) [1] 2 0 2 0 1 0 2 0 3 0 9 0 1 0 7 0 > rr <-with(r, lengths * values) > rr[rr > 0] [1] 2 2 1 2 3 9 1 7 See ?rle for more detials. HTH, Jorge.- On Mon, Jul 16, 2012 at 12:27 PM, jcrosbie <ja...@crosb.ie> wrote: > Thank you, That was very helpful. > > I do have another problem along the same lines. But I can not think of a > way > to do this with a function like ddply or aggregate. > > Example: > x = sample(0:1,42,TRUE) > [1] 1 1 1 1 0 1 0 0 1 1 1 1 1 0 0 0 1 0 0 1 1 0 0 0 0 1 1 0 0 0 1 0 1 1 0 1 > 1 1 0 0 0 0 > I want to find create a new vector such that the sums of the 1's stops each > time there is a 0 and starts again next time there is a one. > Output would be: > 4,1, 5, 1,2,2,1,2,3 > > > > -- > View this message in context: > http://r.789695.n4.nabble.com/remove-loop-which-compares-row-i-to-row-i-1-tp4635327p4636662.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help@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. > [[alternative HTML version deleted]] ______________________________________________ R-help@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.