2

Let's say I have a vector x:

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

How do I identify sets of the NAs within this vector, i.e.,

na_set <- c(1, 1, 0, 0, 2, 2, 0, 0)

My end goal is to use it with a pipe on a data frame using dplyr. So, if there's a function compatible with dplyr that's even better.

Thank you!

jay.sf
  • 60,139
  • 8
  • 53
  • 110
No.Clue
  • 85
  • 1
  • 5
  • 1
    Related dupe-oid (runs of zero instead of `NA`): [Create counter of consecutive runs of a certain value](https://stackoverflow.com/questions/27077228/create-counter-of-consecutive-runs-of-a-certain-value) – Henrik Feb 18 '19 at 23:54

3 Answers3

6

You can take the diff of is.na(x). This will be 1 IFF the element is TRUE and the previous element is FALSE. After applying == 1, you have a logical vector which is TRUE for NA-group starts. Then you can take the cumsum to identify which NA-group you're in, and multiply by is.na(x) to set ne non-NAs to 0.

cumsum(diff(is.na(c(1, x))) == 1)*is.na(x)
#[1] 1 1 0 0 2 2 0 0

Intermediate results displayed:

a <- is.na(c(1, x))
a
#[1] FALSE  TRUE  TRUE FALSE FALSE  TRUE  TRUE FALSE FALSE
b <- diff(a) == 1
b
#[1]  TRUE FALSE FALSE FALSE  TRUE FALSE FALSE FALSE
d <- cumsum(b)
d
#[1] 1 1 1 1 2 2 2 2

I was interested so I did a benchmark. I don't think the results matter practically though, the difference is in milliseconds even for length(x) of 1e7.

x <- c(NA,NA, 1,2,NA,NA, 3,4)
x <- sample(x, 1e7, T)

f_rleid <- function(x){
  nax <- is.na(x)
  r <- rleid(x)*nax
  r[nax] <- rleid(r[nax])
  r
}

f_rle <- function(x){
  r <- rle(is.na(x))
  r$values <- cumsum(r$values) * r$values
  inverse.rle(r)
}

f_diffna <- function(x){
  nax <- is.na(x)
  cumsum(c(as.integer(nax[1]), diff(nax)) == 1L)*nax
}

all.equal(f_rleid(x), f_rle(x))
# [1] TRUE
all.equal(f_rleid(x), f_diffna(x))
# [1] TRUE

microbenchmark::microbenchmark(f_rleid(x), f_rle(x),f_diffna(x))

# Unit: milliseconds
#         expr      min       lq     mean   median       uq      max neval
#   f_rleid(x) 421.9483 437.3314 469.3564 446.5081 511.9315 582.5812   100
#     f_rle(x) 451.3790 519.5278 560.8057 572.4148 591.7632 697.2100   100
#  f_diffna(x) 248.3631 267.5462 315.6224 291.5910 362.8829 459.6873   100
IceCreamToucan
  • 28,083
  • 2
  • 22
  • 38
5

Compute the run length encoding of is.na(x) and replace the values with sequence numbers or 0. Then invert back.

r <- rle(is.na(x))
r$values <- cumsum(r$values) * r$values
inverse.rle(r)
## [1] 1 1 0 0 2 2 0 0
G. Grothendieck
  • 254,981
  • 17
  • 203
  • 341
2

If the exact rank of sets is not a concern, you can use the convenient rleid() function from data.table:

rleid(x) * is.na(x)

[1] 1 1 0 0 4 4 0 0

For comparison of speed:

library(microbenchmark)

x <- rep(x, 1e5)
microbenchmark(
 IceCreamToucan = cumsum(diff(is.na(c(1, x))) == 1)*is.na(x),
 tmfmnk = rleid(x) * is.na(x),
 G._Grothendieck = {r <- rle(is.na(x))
 r$values <- cumsum(r$values) * r$values
 inverse.rle(r)},
 times = 5
)

Unit: milliseconds
            expr       min       lq     mean   median       uq      max neval cld
  IceCreamToucan 48.607317 52.49508 66.64196 74.63182 76.81896 80.65662     5   b
          tmfmnk  9.952486 12.58168 20.22834 14.38625 16.23961 47.98166     5  a 
 G._Grothendieck 53.533149 57.48818 59.12514 59.73295 62.14772 62.72371     5   b
tmfmnk
  • 38,881
  • 4
  • 47
  • 67