[R] consecutive numbering of elements in a matrix

William Dunlap wdunlap at tibco.com
Sat Nov 21 21:25:07 CET 2009


> -----Original Message-----
> From: r-help-bounces at r-project.org 
> [mailto:r-help-bounces at r-project.org] On Behalf Of Jim Bouldin
> Sent: Saturday, November 21, 2009 10:34 AM
> To: r-help at r-project.org
> Subject: [R] consecutive numbering of elements in a matrix
> 
> 
> 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). 

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  



> 
> Any ideas?
> Thanks
> 
> 
> Jim Bouldin, PhD
> Research Ecologist
> Department of Plant Sciences, UC Davis
> Davis CA, 95616
> 530-554-1740
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide 
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
> 




More information about the R-help mailing list