Skip to content
Prev 295501 / 398506 Next

Using NA as a break point for indicator variable?

Hello,

Assuming that 'd' is your original data.frame and that you've set entire 
rows to NA, try this


d$leak_num <- NA
ix <- !is.na(d[, 1])  # any column will do, entire row is NA
## alternative, if other rows may have NAs, due to something else
#ix <- apply(d, 1, function(x) all(!is.na(x)))
r <- rle(ix)
v <- cumsum(r$values)
d$leak_num[ix] <- rep(v[r$values], r$lengths[r$values])
d


Hope this helps,

Rui Barradas

Em 24-05-2012 11:00, Max Brondfield <mbrondf at post.harvard.edu> escreveu: