Skip to content
Prev 201101 / 398503 Next

consecutive numbering of elements in a matrix

Is this what you are looking for?
  > numberNonNAsInColumn <- function (A) {
      for (i in seq_len(ncol(A))) {
          isNotNA <- !is.na(A[, i])
          A[isNotNA, i] <- seq_len(sum(isNotNA))
      }
      A
  }
  > numberNonNAsInColumn(A)
       [,1] [,2] [,3]
  [1,]    1   NA   NA
  [2,]    2   NA   NA
  [3,]    3    1    1
  [4,]    4    2    2
  [5,]    5    3    3
  [6,]    6    4    4
  [7,]    7    5   NA
  > numberNonNAsInColumn(cbind(c(101,NA,102,103,NA,NA,104),
c(1001,1002,1003,NA,1004,1005,1006)))
       [,1] [,2]
  [1,]    1    1
  [2,]   NA    2
  [3,]    2    3
  [4,]    3   NA
  [5,]   NA    4
  [6,]   NA    5
  [7,]    4    6
I didn't know what you wanted to do if there were NA's
in the middle of a column.

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com