2

thank you for your help in advance. I am new to R, so please overlook any glaring errors in my code.

I am trying to replace each non-zero value in a matrix with arrays that I created. Here is what I have so far:

totalsquares<-matrix(data=rbinom(225,1,0.65), nrow=15, ncol=15, byrow=TRUE)
samplesquares <- sum(totalsquares)
totalsquares[totalsquares == 1] <- rpois(samplesquares,30)
urchins<-sum(totalsquares)
for(rowcount in 1:15){
  for(colcount in 1:15){
    if(totalsquares[rowcount, colcount] != 0){
      capprob<-rbeta(1,0.48,0.12)
      cmr<-array(data=rbinom((5*totalsquares[rowcount,colcount]), 1, capprob), c(totalsquares[rowcount, colcount],5,samplesquares))
    }
  }
}

The idea here was to create a grid representing my sampling area, then I'd sample approximately 65% of it. Each square where I'd sample, I'd have some number of urchins based off a density of about 30/square.

The loop is to create encounter histories for a capture-mark-recapture model. It is supposed to create an array of binomial data for each sampled square, with the dimensions being the number of sampled individuals by 5 occasions by the number of the square.

The main problem is that I can't figure out how to replace the values in the matrix with the array representing that sample. I also don't think my loop is working properly, as it keeps having the same number of rows for each array when they should be different for each.

Any help is appreciated!

  • You want the end result to be a 15x15 matrix, but some of the entries of that matrix will hold a scalar (like 0) and others will hold an array? I don't think you can make it work for a matrix (which needs to have all elements of the same type), but you could maybe hack it with a multidimensional list (which will tolerate different types for different elements) http://stackoverflow.com/a/13568656/288545 – Daniel Kessler Nov 13 '15 at 20:06

0 Answers0