Skip to content
Prev 295513 / 398506 Next

Using NA as a break point for indicator variable?

You can count the cumulative number of NA breakpoints in a vector
with cumsum(is.na(vector)), as in

  > cbind(d, LeakNo=with(d, cumsum(is.na(lon)|is.na(lat)|is.na(CH4))))
           lon      lat      CH4 LeakNo
  1  -71.11954 42.35068 2.595834      0
  2  -71.11954 42.35068 2.595688      0
  3         NA       NA       NA      1
  4         NA       NA       NA      2
  5         NA       NA       NA      3
  6  -71.11948 42.35068 2.435762      3
  7  -71.11948 42.35068 2.491003      3
  8         NA       NA       NA      4
  9  -71.11930 42.35068 2.464475      4
  10 -71.11932 42.35068 2.470865      4

Add 1 if you want to start with 1.  If you only want to increase the count
after each sequence of NA's then you could use rle() or
  > na <- with(d, is.na(lon)|is.na(lat)|is.na(CH4))
  > cbind(d, LeakNo=cumsum(c(TRUE, na[-1] < na[-length(na)])))
           lon      lat      CH4 LeakNo
  1  -71.11954 42.35068 2.595834      1
  2  -71.11954 42.35068 2.595688      1
  3         NA       NA       NA      1
  4         NA       NA       NA      1
  5         NA       NA       NA      1
  6  -71.11948 42.35068 2.435762      2
  7  -71.11948 42.35068 2.491003      2
  8         NA       NA       NA      2
  9  -71.11930 42.35068 2.464475      3
  10 -71.11932 42.35068 2.470865      3

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com