I have an array l <- c(1,1,1,1, 0,0,0,0,0, 1,1,1,1,1,1) I would like to get a rugged array [[1]] 1,1,1,1 [[2]] 0,0,0,0,0 [[3]] 1,1,1,1,1,1 catching every group of contiguous repeated values. Any help would be greatly appreciated! -Pavel -- View this message in context: http://r.789695.n4.nabble.com/split-array-into-groups-by-value-tp4448578p4448578.html Sent from the R help mailing list archive at Nabble.com.
Hello, pavlo wrote> > I have an array > l <- c(1,1,1,1, 0,0,0,0,0, 1,1,1,1,1,1) > > I would like to get a rugged array > [[1]] 1,1,1,1 > [[2]] 0,0,0,0,0 > [[3]] 1,1,1,1,1,1 > > catching every group of contiguous repeated values. Any help would be > greatly appreciated! > -Pavel >Is this it? x <- c(1,1,1,1, 0,0,0,0,0, 1,1,1,1,1,1) contig <- cumsum(abs(diff(c(x[1], x)))) split(x, contig) Hope this helps, Rui Barradas -- View this message in context: http://r.789695.n4.nabble.com/split-array-into-groups-by-value-tp4448578p4448622.html Sent from the R help mailing list archive at Nabble.com.
One approach is: l <- c(1,1,1,1, 0,0,0,0,0, 1,1,1,1,1,1) r <- rle(l)$lengths split(l, rep(seq_along(r), r)) I hope it helps. Best, Dimitris On 3/6/2012 3:53 AM, pavlo wrote:> I have an array > l<- c(1,1,1,1, 0,0,0,0,0, 1,1,1,1,1,1) > > I would like to get a rugged array > [[1]] 1,1,1,1 > [[2]] 0,0,0,0,0 > [[3]] 1,1,1,1,1,1 > > catching every group of contiguous repeated values. Any help would be > greatly appreciated! > -Pavel > > > > -- > View this message in context: http://r.789695.n4.nabble.com/split-array-into-groups-by-value-tp4448578p4448578.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/
Rui and Dimitris, thank you much, both approaches work great! (although rle won me, I had a feeling something like that had to exist) -- View this message in context: http://r.789695.n4.nabble.com/split-array-into-groups-by-value-tp4448578p4451048.html Sent from the R help mailing list archive at Nabble.com.