Skip to content
Prev 274219 / 398506 Next

exclude columns with at least three consecutive zeros

First define a function that returns TRUE if a column
should be dropped.  E.g.,

  has3Zeros.1 <- function(x) 
  {
      x <- x[!is.na(x)] == 0 # drop NA's, convert 0's to TRUE, others to FALSE
      if (length(x) < 3) {
          FALSE # you may want to further test short vectors
      } else {
          i <- seq_len(length(x) - 2)
          any(x[i] & x[i + 1] & x[i + 2])
      }
  }

or

  has3Zeros.2 <- function (x) 
  {
      x <- x[!is.na(x)] == 0
      r <- rle(x)
      any(r$lengths[r$values] >= 3)
  }

The use sapply on your data.frame with this function to see which
columns to omit and use [ to omit them:
  > e <- data.frame(Date=1980:1985,
  +                 A = c(2, 9, 18, 0, 12, 48),
  +                 B = c(75, NA, 15, 16, 43, 3),
  +                 C = c(12, 7, 0, 0, 0, 26),
  +                 D = c(41, 0, 0, NA, 0, 21))
  > e[, !sapply(e, has3Zeros.1), drop=FALSE]
    Date  A  B
  1 1980  2 75
  2 1981  9 NA
  3 1982 18 15
  4 1983  0 16
  5 1984 12 43
  6 1985 48  3

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com