0

I am preparing a dataset to run a SVM classification. So far, I have a raster stack which includes a layer ([6]) for training

> S1
class       : RasterStack 
dimensions  : 3865, 6899, 26664635, 6  (nrow, ncol, ncell, nlayers)
resolution  : 14.83, 14.83  (x, y)
extent      : 361363.5, 463675.7, 5760647, 5817965  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=utm +zone=32 +datum=WGS84 +units=m +no_defs +ellps=WGS84 +towgs84=0,0,0 
names       : Coherence_VV_Stack2.1, Coherence_VV_Stack2.2, Coherence_VV_Stack2.3, Coherence_VV_Stack2.4, Coherence_VV_Stack2.5, Class 
min values  :                     ?,                     ?,                     ?,                     ?,                     ?,     0 
max values  :                     ?,                     ?,                     ?,                     ?,                     ?,     1 

Where S1[[6]] is:

> S1[[6]]
class       : RasterLayer 
dimensions  : 3865, 6899, 26664635  (nrow, ncol, ncell)
resolution  : 14.83, 14.83  (x, y)
extent      : 361363.5, 463675.7, 5760647, 5817965  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=utm +zone=32 +datum=WGS84 +units=m +no_defs +ellps=WGS84 +towgs84=0,0,0 
data source : in memory
names       : Class 
values      : 0, 1  (min, max)

and plot(S1[[6]) is:

enter image description here

I want to create a dataframe (to be used later as input for classification) where:

  • If pixel value in S1[[6]] is 1, extract the pixel values in the other layers of the raster stack and put it in the dataframe

  • If pixel value in S1[[6]] is0, do nothing.

Any suggestion on how to proceed? I know the issue can be solved by importing a shapefile and use the extract function but I want to use this approach.

  • What columns do you want in your data frame? – Spacedman Sep 17 '18 at 11:28
1

Given a 3x4 cell x 6 layer brick where the last layer is a 0/1 indicator:

> S1
class       : RasterBrick 
dimensions  : 3, 4, 12, 6  (nrow, ncol, ncell, nlayers)
resolution  : 0.25, 0.3333333  (x, y)
extent      : 0, 1, 0, 1  (xmin, xmax, ymin, ymax)
coord. ref. : NA 
data source : in memory
names       :    layer.1,    layer.2,    layer.3,    layer.4,    layer.5,    layer.6 
min values  : 0.07979194, 0.01004478, 0.12462376, 0.08388173, 0.03662221, 0.00000000 
max values  :  0.8417125,  0.9823274,  0.9087239,  0.9050184,  0.9624438,  1.0000000 

Then this one-liner will return a data frame of the values in the first five layers for cells that have a one in the last layer:

> ones = setNames(do.call(cbind.data.frame,
          lapply(1:nlayers(S1),
                 function(i){S1[[i]][S1[[nlayers(S1)]][]==1]})),
                 names(S1))
> ones
     layer.1    layer.2   layer.3    layer.4    layer.5 layer.6
1 0.61572701 0.30333021 0.4982907 0.18118309 0.03662221       1
2 0.84171251 0.98232742 0.1246238 0.40075522 0.68438716       1
3 0.37735819 0.27628720 0.7060747 0.25431005 0.33987833       1
4 0.30291850 0.10383452 0.2485474 0.90501841 0.30548540       1
5 0.57979692 0.01196571 0.4019636 0.36540125 0.96244376       1
6 0.14576374 0.48495665 0.4268066 0.34438584 0.83443135       1
7 0.70245897 0.01004478 0.9087239 0.78135612 0.19428952       1
8 0.79984520 0.24450234 0.1956386 0.08388173 0.07563230       1
9 0.07979194 0.32003334 0.3989493 0.48715195 0.18219895       1

To check, the first column should be the numbers in here:

> as.matrix(S1[[1]])
          [,1]      [,2]      [,3]       [,4]
[1,] 0.6157270 0.8417125 0.3773582 0.30291850
[2,] 0.5797969 0.1457637 0.3296398 0.46653010
[3,] 0.7024590 0.7198337 0.7998452 0.07979194

that have a one in here:

> as.matrix(S1[[6]])
     [,1] [,2] [,3] [,4]
[1,]    1    1    1    1
[2,]    1    1    0    0
[3,]    1    0    1    1

Building tiny examples is a very useful skill when developing data analysis methods.

  • What if I want to extract the values when S1[[6]] is 1 AND 0. Would it be something like S1[[6]]==1&&0 or S1[[6]]==1 && S1[[6]]==0? – GCGM Sep 18 '18 at 11:01
  • 1
    A single value in S1[[6]] can't be both 1 AND 0 - you need a conditional OR test. z = c(0, 1, 2, NA) then (z == 1) | (z == 0). Might want to create the function outside the one-liner for testing purposes. – Spacedman Sep 18 '18 at 11:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.