[R] consecutive numbering of elements in a matrix

Dimitris Rizopoulos d.rizopoulos at erasmusmc.nl
Sat Nov 21 20:50:58 CET 2009


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

-- 
Dimitris Rizopoulos
Assistant Professor
Department of Biostatistics
Erasmus University Medical Center

Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands
Tel: +31/(0)10/7043478
Fax: +31/(0)10/7043014




More information about the R-help mailing list