Skip to content

consecutive numbering of elements in a matrix

4 messages · Jim Bouldin, Dimitris Rizopoulos, William Dunlap +1 more

#
Within a very large matrix composed of a mix of values and NAs, e.g, matrix A:

     [,1] [,2] [,3]
[1,]    1   NA   NA
[2,]    3   NA   NA
[3,]    3   10   17
[4,]    4   12   18
[5,]    6   16   19
[6,]    6   22   20
[7,]    5   11   NA

I need to be able to consecutively number, in new columns, the non-NA
values within each column (i.e. A[1,1] A[3,2] and A[3,3] would all be set
to one, and subsequent values in those columns would increase by one, until
the last non-NA value is reached, if any). 

Any ideas?
Thanks


Jim Bouldin, PhD
Research Ecologist
Department of Plant Sciences, UC Davis
Davis CA, 95616
530-554-1740
#
if I understand what you want correctly, then one approach is:

A <- matrix(sample(50, 21), 7, 3)
A[sample(21, 5)] <- NA

A
row(A) - apply(is.na(A), 2, cumsum)

I hope it helps.

Best,
Dimitris
Jim Bouldin wrote:

  
    
#
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
#
On Nov 21, 2009, at 3:25 PM, William Dunlap wrote:

            
mtx <- matrix(scan(textConnection(" 1   NA   NA
     3   NA   NA
     3   10   17
     4   12   18
     6   16   19
     6   22   20
     5   11   NA"), byrow=TRUE, ncol=3)
Semms like a simple apply would be sufficient:

apply(mtx, 2, function(x) cumsum(!is.na(x)))

      [,1] [,2] [,3]
[1,]    1    0    0
[2,]    2    0    0
[3,]    3    1    1
[4,]    4    2    2
[5,]    5    3    3
[6,]    6    4    4
[7,]    7    5    4

Or if NA's are needed in the original NA positions:

 > mt2 <- apply(mtx, 2, function(x) cumsum(!is.na(x)))
 > is.na(mt2) <- is.na(mtx)
 > mt2
      [,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
David Winsemius, MD
Heritage Laboratories
West Hartford, CT