[R] consecutive numbering of elements in a matrix

David Winsemius dwinsemius at comcast.net
Sat Nov 21 21:38:23 CET 2009


On Nov 21, 2009, at 3:25 PM, William Dunlap wrote:

>> -----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

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)

>>
>> 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
>  }

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

>> 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.
>>
>
> ______________________________________________
> 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.

David Winsemius, MD
Heritage Laboratories
West Hartford, CT




More information about the R-help mailing list