[R] count length of continues elements in a vector

Marc Schwartz marc_schwartz at me.com
Tue Jun 7 22:37:26 CEST 2011


On Jun 7, 2011, at 9:25 AM, davetracz wrote:

> I am performing a precipitation analysis. data is in the form of daily
> precipitation amounts, e.g.
> 
> x<- c(4,5,3,0,0,0,2,4,6,4,0,0,0,2,2,0,3,4,1,0,...)
> 
> I would like to find the length of the "storm", length of storm would be
> defined as the number of days with continues precipitation. in this case the
> returned vector would be:
> 
> (3,4,2,3,...)
> 
> I would also like the amount of precipitation associated with each "storm"
> in another variable. in this case the variable would look like:
> 
> (12,16,4,8,...)
> 
> any suggestions would be appreciated.
> 
> thanks
> Dave


?rle will be useful here

x <- c(4, 5, 3, 0, 0, 0, 2, 4, 6, 4, 0, 0, 0, 2, 2, 0, 3, 4, 1, 0)

# Get the runs of values in 'x' >= 1
R1 <- rle(x >= 1)

> R1
Run Length Encoding
  lengths: int [1:8] 3 3 4 3 2 1 3 1
  values : logi [1:8] TRUE FALSE TRUE FALSE TRUE FALSE ...


# Split 'x' by the sequences of values >= 1 in 'R1'
# The values will be grouped by integers, including '0s'
# > rep(cumsum(R1$values) * R1$values, R1$lengths)
#  [1] 1 1 1 0 0 0 2 2 2 2 0 0 0 3 3 0 4 4 4 0
# so we want to remove the first '0' group
R2 <- split(x, rep(cumsum(R1$values) * R1$values, R1$lengths))[-1]
 

> R2
$`1`
[1] 4 5 3

$`2`
[1] 2 4 6 4

$`3`
[1] 2 2

$`4`
[1] 3 4 1


Now use normal list processing to get the lengths and sums


> sapply(R2, length)
1 2 3 4 
3 4 2 3 

> sapply(R2, sum)
 1  2  3  4 
12 16  4  8 


HTH,

Marc Schwartz



More information about the R-help mailing list