[R] split array into groups by value

Rui Barradas rui1174 at sapo.pt
Tue Mar 6 04:17:21 CET 2012


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.



More information about the R-help mailing list