简体   繁体   中英

Subset raster by highest XY percentage of value

I want to do something equivalent of subset by quantile for dataframe, but for raster. Basically I want to know where are the highest 20% of values in my raster located and create a new raster containing only those cells. Something in this regard:

xy <- raster(matrix(rnorm(400),20,20))

xy_df <- as.data.frame(xy)
xy_df <- subset(xy_df, layer <= quantile(layer, 0.2, na.rm = TRUE))

This subsets 20% of lowest values from the dataframe, but I would like to subset 20% of highest values from raster.

Thank you in advance.

An object created with the raster function has a complex S4 structure. Here's a portion of the output of str on xy:

 str(xy)
#----------------------
Formal class 'RasterLayer' [package "raster"] with 12 slots
  ..@ file    :Formal class '.RasterFile' [package "raster"] with 13 slots
  .. .. ..@ name        : chr ""
  .. .. ..@ datanotation: chr "FLT4S"
  .. .. ..@ byteorder   : chr "little"
  .. .. ..@ nodatavalue : num -Inf
  .. .. ..@ NAchanged   : logi FALSE
  .. .. ..@ nbands      : int 1
  .. .. ..@ bandorder   : chr "BIL"
  .. .. ..@ offset      : int 0
  .. .. ..@ toptobottom : logi TRUE
  .. .. ..@ blockrows   : int 0
  .. .. ..@ blockcols   : int 0
  .. .. ..@ driver      : chr ""
  .. .. ..@ open        : logi FALSE
  ..@ data    :Formal class '.SingleLayerData' [package "raster"] with 13 slots
  .. .. ..@ values    : num [1:400] -0.00111 0.30574 1.15131 0.62849 -0.75049 ...
  .. .. ..@ offset    : num 0
  .. .. ..@ gain      : num 1
  .. .. ..@ inmemory  : logi TRUE
  .. .. ..@ fromdisk  : logi FALSE
  .. .. ..@ isfactor  : logi FALSE
  .. .. ..@ attributes: list()
  #............... remainder omitted

Both the values and "[" functions can retrieve the @values sub-slot of the @data slot. So perhaps you wanted (recognizing that this question has remained unanswered and uncommented for 2 yea

rs):

   raster( as.matrix(values(xy)[ values(xy) >= quantile(values(xy), 0.8, na.rm=TRUE)] ))
class       : RasterLayer 
dimensions  : 80, 1, 80  (nrow, ncol, ncell)
resolution  : 1, 0.0125  (x, y)
extent      : 0, 1, 0, 1  (xmin, xmax, ymin, ymax)
coord. ref. : NA 
data source : in memory
names       : layer 
values      : 0.7850559, 3.600096  (min, max)

What wasn't clear was the expected structure of the raster. The origianl xy had dimensions inherited from its matrix. Coercing the numeric vector to a matrix without any dimensions leaves it "one-dimensional".

You can do this

library(raster)
r <- raster(matrix(rnorm(400),20,20))
q <- quantile(r, 0.8)
x <- reclassify(r, cbind(-Inf, q, NA))

q
#      80% 
#0.7833264 
x
#class       : RasterLayer 
#dimensions  : 20, 20, 400  (nrow, ncol, ncell)
#resolution  : 0.05, 0.05  (x, y)
#extent      : 0, 1, 0, 1  (xmin, xmax, ymin, ymax)
#coord. ref. : NA 
#data source : in memory
#names       : layer 
#values      : 0.786266, 2.45623  (min, max)

plot(x)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM