[R] changing equal values on matrix by same random number

David Huffer David.Huffer at csosa.gov
Wed Aug 26 19:07:18 CEST 2009


You want to replace all 1s each with the same random nuumber from the uniform distribution, then all 2s each with the same random nuumber from the uniform distribution, and so forth?  Are the arguments (i.e., the min and max of the distribution) to each call to runif identical?

--
 David
 
 -----------------------------------------------------
 David Huffer, Ph.D.               Senior Statistician
 CSOSA/Washington, DC           david.huffer at csosa.gov
 -----------------------------------------------------

-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of milton ruser
Sent: Wednesday, August 26, 2009 12:54 PM
To: r-help at r-project.org
Subject: [R] changing equal values on matrix by same random number

Dear all,

I have about 30,000 matrix (512x512), with values from 1 to N.
Each value on a matrix represent a habitat patch on my
matrix (i.e. my landscape). Non-habitat are stored as ZERO.
No I need to change each 1-to-N values for the same random
number.

Just supose my matrix is:
mymat<-matrix(c(1,1,1,0,0,0,0,0,0,0,0,
0,0,0,0,2,2,2,0,0,0,0,
0,0,0,0,2,2,2,0,0,0,0,
3,3,0,0,0,0,0,0,0,4,4,
3,3,0,0,0,0,0,0,0,0,0), nrow=5)

I would like that all cells with 1 come to be
runif(1,min=0.4, max=0.7), and cells with 2
be replace by another runif(...).

I can do it using for(), but it is very time expensive.
Any help are welcome.

cheers

milton

	[[alternative HTML version deleted]]

______________________________________________
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