[R] consecutive numbering of elements in a matrix

Jim Bouldin jrbouldin at ucdavis.edu
Sat Nov 21 19:34:29 CET 2009


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




More information about the R-help mailing list