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.
Have a look at the buffer function in the adehabitat package. HTH, Thierry ------------------------------------------------------------------------ ---- ir. Thierry Onkelinx Instituut voor natuur- en bosonderzoek / Research Institute for Nature and Forest Cel biometrie, methodologie en kwaliteitszorg / Section biometrics, methodology and quality assurance Gaverstraat 4 9500 Geraardsbergen Belgium tel. + 32 54/436 185 Thierry.Onkelinx op inbo.be www.inbo.be To call in the statistician after the experiment is done may be no more than asking him to perform a post-mortem examination: he may be able to say what the experiment died of. ~ Sir Ronald Aylmer Fisher The plural of anecdote is not data. ~ Roger Brinner The combination of some data and an aching desire for an answer does not ensure that a reasonable answer can be extracted from a given body of data. ~ John Tukey -----Oorspronkelijk bericht----- Van: r-help-bounces op r-project.org [mailto:r-help-bounces op r-project.org] Namens ACroske Verzonden: vrijdag 18 juli 2008 20:24 Aan: r-help op r-project.org Onderwerp: [R] "Spreading risk" in a matrix 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-tp18535227p185352 27.html Sent from the R help mailing list archive at Nabble.com. ______________________________________________ R-help op 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.
How do I use the buffer function since I have more than the two columns it calls for? (I have 46!) -- View this message in context: http://www.nabble.com/%22Spreading-risk%22-in-a-matrix-tp18535227p18537082.html Sent from the R help mailing list archive at Nabble.com.
Buffer works on a 2D dataset like a matrix. Have a look at the examples in ?buffer. But you probably will have to create a set of coordinaten of the points you want to buffer. HTH, Thierry ------------------------------------------------------------------------ ---- ir. Thierry Onkelinx Instituut voor natuur- en bosonderzoek / Research Institute for Nature and Forest Cel biometrie, methodologie en kwaliteitszorg / Section biometrics, methodology and quality assurance Gaverstraat 4 9500 Geraardsbergen Belgium tel. + 32 54/436 185 Thierry.Onkelinx op inbo.be www.inbo.be To call in the statistician after the experiment is done may be no more than asking him to perform a post-mortem examination: he may be able to say what the experiment died of. ~ Sir Ronald Aylmer Fisher The plural of anecdote is not data. ~ Roger Brinner The combination of some data and an aching desire for an answer does not ensure that a reasonable answer can be extracted from a given body of data. ~ John Tukey -----Oorspronkelijk bericht----- Van: r-help-bounces op r-project.org [mailto:r-help-bounces op r-project.org] Namens ACroske Verzonden: vrijdag 18 juli 2008 22:22 Aan: r-help op r-project.org Onderwerp: Re: [R] "Spreading risk" in a matrix How do I use the buffer function since I have more than the two columns it calls for? (I have 46!) -- View this message in context: http://www.nabble.com/%22Spreading-risk%22-in-a-matrix-tp18535227p185370 82.html Sent from the R help mailing list archive at Nabble.com. ______________________________________________ R-help op 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.