Get run lengths of missing values ​​in vector

What is a smart (i.e. not a cycle) way to get the length of each spell of missing values ​​in a vector? My ideal output is a vector that has the same length in which each missing value is replaced by the length of the missing value spell of which it is a part, and all other values ​​are 0.

So for type input:

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

I would like, for example, the conclusion:

y <- c(0,0,0,0,3,3,3,0,0,2,2)
+4
source share
3 answers

One simple option using rle:

m <- rle(is.na(x))
> rep(ifelse(m$values,m$lengths,0),times = m$lengths)
[1] 0 0 0 0 3 3 3 0 0 2 2
+9
source

-, rle() cumsum() dplyr group_by() n() NA:

> x2 <- as.numeric(is.na(x))
  0 0 0 0 1 1 1 0 0 1 1

> rle(x2)
Run Length Encoding
  lengths: int [1:4] 4 3 2 2
  values : num [1:4] 0 1 0 1

# Now we can assign group-numbers...
> cumsum(c(diff(x2)==+1,0)) * x2
  0 0 0 0 1 1 1 0 0 2 2
# ...then get group-lengths from counting those...
> rle(cumsum(c(diff(x2)==+1,0)) * x2)
Run Length Encoding
  lengths: int [1:4] 4 3 2 2
  values : num [1:4] 0 1 0 2

- , , @joran.

+1

Here is another option with rleidandave

library(data.table)
ave(x, rleid(is.na(x)), FUN = length)*is.na(x)
#[1] 0 0 0 0 3 3 3 0 0 2 2
+1
source

Source: https://habr.com/ru/post/1672863/


All Articles