Re: [R] "Spreading risk" in a matrix

From: ONKELINX, Thierry <>
Date: Fri, 18 Jul 2008 21:43:15 +0200

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
tel. + 32 54/436 185

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: [] Namens ACroske
Verzonden: vrijdag 18 juli 2008 20:24
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):


binary.matrix<-matrix(rbinom(length(prob.matrix),prob=prob.matrix,size=1 ),nrow=nrow(prob.matrix))


       for (i in 1:N){
            if (res[i]==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:
Sent from the R help mailing list archive at

______________________________________________ mailing list
PLEASE do read the posting guide
and provide commented, minimal, self-contained, reproducible code.

______________________________________________ mailing list
PLEASE do read the posting guide
and provide commented, minimal, self-contained, reproducible code.
Received on Fri 18 Jul 2008 - 19:45:30 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Sat 19 Jul 2008 - 01:31:39 GMT.

Mailing list information is available at Please read the posting guide before posting to the list.

list of date sections of archive