[R] "Spreading risk" in a matrix
ACroske
Audy3272 at yahoo.com
Fri Jul 18 20:24:01 CEST 2008
I have a binary matrix that represents a map of invasive species risk (1 =
infested; 0 = uninfested). It started as a matrix of probabilities
(developed in ArcMap) that I converted to binary with this R code:
binary.matrix<-matrix(rbinom(length(prob.matrix),prob=prob.matrix,size=1),nrow=nrow(prob.matrix))
Now, I would like to "spread" the risk for year 2 of the invasion. I'd like
to do this by location, so, for every cell that is currently a 1, next year
it's immediate neighbors (above, below, to the right and left) would also
become ones. Here is a function I came up with to attempt this (clearly, my
function writing is rough, to say the least):
matrix.function<-function(N){
binary.matrix<-matrix(rbinom(length(prob.matrix),prob=prob.matrix,size=1),nrow=nrow(prob.matrix))
res<-numeric(N)
for (i in 1:N){
res[i]<-binary.matrix
if (res[i]==1){
res[,i+1]=1
res[,i-1]=1
}
}
}
When I run this code, I get many messages saying "In res[i]<-binary.matrix :
number of items to replace is not a multiple of replacement length"
I know that if I actually got this to work, it would be a string of numbers
(because of the "numeric"), but I'm just working with my limited
function-writing knowledge, and I assumed it would be possible to convert
the results to a matrix later on.
I'm aware that this function is probably a disaster, but that's why I'm
here!
--
View this message in context: http://www.nabble.com/%22Spreading-risk%22-in-a-matrix-tp18535227p18535227.html
Sent from the R help mailing list archive at Nabble.com.
More information about the R-help
mailing list