Jens Oldeland
2009-Jun-30 11:41 UTC
[R] sampling quadrats of increasing size in a list of matrices
Dear R-Users, I have a problem and I have already surveyed different packages especially in the Spatial-TASK view but did not find something applicable. :( I am looking for a solution on how to sample increasing sizes of "quadrats" in a list of matrices. Each matrix is a binary raster map and I want to know if there is a 1 or only 0 s in the sampling unit (quadrat, e.g. 4x4 cells). This block has to vary in size so that each time the area of the matrix (think of a map) is sampled, the areas I am looking at are becoming larger. That means, in the first run cellsize would 1x1 than 2x2 than 4x4 than 8x8 than 16x16 up to 64x64 which is the final size of a matrix. Furthermore, I need this done on a list object consisting of a set of matrices, all with the same size (64x64). The list is 100 objects "tall". The result should be a again a vector for each matrix with the length of possible steps to sample the quadrat. This vector should report for each sampling unit a 1 if there is a 1 somewhere otherwise the value should be zero. e.g. this is matrix number 90 from my list ( only showing a subset from 16 x16) [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14] [,15] [,16] [1,] 0 0 1 1 1 0 1 1 1 1 1 0 0 0 1 1 [2,] 0 1 1 1 0 1 0 1 1 0 1 1 1 1 1 1 [3,] 1 0 1 1 1 1 0 1 1 1 1 1 0 1 0 1 [4,] 0 0 0 0 1 0 0 1 1 0 1 1 1 1 1 1 [5,] 1 1 1 1 1 0 0 1 0 1 1 0 0 0 0 0 [6,] 1 0 1 1 0 0 0 0 0 1 1 1 1 0 1 1 [7,] 1 1 0 1 1 1 1 1 1 1 1 1 1 0 1 0 [8,] 1 1 1 1 1 0 1 0 1 1 1 0 1 1 1 1 [9,] 1 0 1 0 1 1 1 1 1 1 0 1 1 1 1 1 [10,] 1 1 1 1 1 1 1 0 1 0 0 0 0 1 1 1 [11,] 0 1 0 1 1 1 0 1 1 0 0 1 1 1 1 1 [12,] 1 1 1 1 0 1 1 1 1 1 0 1 1 1 0 0 [13,] 1 1 1 1 1 1 1 0 0 1 1 1 1 1 0 0 [14,] 1 1 0 1 1 0 0 1 0 0 1 1 1 0 1 1 [15,] 0 1 1 1 0 1 1 0 1 1 0 1 0 1 1 1 [16,] 0 1 1 0 1 0 1 1 1 0 1 1 1 1 0 0 with sample size of 2x2 (4 cells) one would need 64 steps to sample all 256 hence the result vector should have a length of 64. I think there is only one spot row 4 col.6,7 row 5 col.6,7 that has only zeros. increasing the sample size to 8x8 would need a vector of length 32, possibly without any zero. the matrices have an decreasing amount of 1s, so the rasters from 1-20 are more dominated by zeros. in a second step; the sampling should be with a random nonoverlapping placement of these sampling units. but I will post this question separately. Your kind help will be highly appreciated! Jens -- +++++++++++++++++++++++++++++++++++++++++ Dipl.Biol. Jens Oldeland University of Hamburg Biocentre Klein Flottbek and Botanical Garden Ohnhorststr. 18 22609 Hamburg, Germany Tel: 0049-(0)40-42816-407 Fax: 0049-(0)40-42816-543 Mail: Oldeland at botanik.uni-hamburg.de Jens.Oldeland at DLR.de (for attachments > 2mb!!) http://www.biologie.uni-hamburg.de/bzf/fbda005/fbda005.htm +++++++++++++++++++++++++++++++++++++++++
Chris Stubben
2009-Jun-30 18:15 UTC
[R] sampling quadrats of increasing size in a list of matrices
Jens Oldeland wrote:> > I am looking for a solution on how to sample increasing sizes of > "quadrats" in a list of matrices. Each matrix is a binary raster map and > I want to know if there is a 1 or only 0 s in the sampling unit > (quadrat, e.g. 4x4 cells). >You could just use a few loops to index the submatrices/quadrats, maybe something like this. set.seed(7) x<-matrix(sample(c(0,1), 256, replace=TRUE, prob=c(.8,.2)), nrow=16, byrow=TRUE) # matrix size (16) n<-nrow(x) #quadrat sizes (1 2 4 8 16) q<- 2^(0:sqrt(n)) # store results in list quads<-vector("list", length(q) ) names(quads)<-q for (i in 1:length(q) ) { z<-c() for(j in 1:(n-q[i]+1) ) { for(k in 1:(n-q[i]+1)) { y<-x[j:(j+q[i]-1), k:(k+q[i]-1)] #do something with submatrix z<-c(z, ifelse( sum(y)>0, 1,0)) #z<-c(z, sum(y) ) } } quads[[i]]<-z } #empty quads sapply(quads, function(x) sum(x==0)) 1 2 4 8 16 206 91 3 0 0 # total number of quadrats sampled sapply(quads, length) #or (n-q+1)^2 For random sampling, try replacing 1:(n-q[i]+1) with sample( 1:(n-q[i]+1), replace=TRUE) ? Also, if you have a list of matrices, then maybe combine the code above into a function and then apply it to your list using lapply. Chris Stubben -- View this message in context: http://www.nabble.com/sampling-quadrats-of-increasing-size-in-a-list-of-matrices-tp24270434p24276950.html Sent from the R help mailing list archive at Nabble.com.