How to count the number of 0s between 1s in R? -
i have precipitation data converted binary, 1 = precipitation event , 0 = no precipitation. data set has on 35,000 values, here example of data this:
x = 1, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1. i count number of consecutive days without event, output this:
y = 2, 3, 6, 2. i have tried use site none of suggested solutions worked.
we can use rle
with(rle(x), lengths[!values]) #[1] 2 3 6 2 if have '0's @ end of vector, can start count first 1 last 1
x1 <- x[reduce(':',as.list(range(which(x==1))))] with(rle(x1), lengths[!values])
Comments
Post a Comment