最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

r - Filter raster layers based on multiple terms - Stack Overflow

programmeradmin7浏览0评论

The terra package supports a very convenient matching method for layer selection for multi-layer raster data:

# expression based (partial) matching of names with single brackets: s['^A']

However, this rule does not take effect for the time being when dealing with multiple conditions, as follows: I want to select all layers whose names start with 'A' and 'C':

library(terra)
data <- rast(nrow = 5, ncol =5, nlyr =6, vals = runif(5*5*6)) |> 
  `names<-`(c('A-1', 'A-2', 'B-1', 'B-2', 'C-1', 'C-2'))

data['^C'] #success

data[c('^A', '^C')]  
#> data[c('^A', '^C')]
#class       : SpatRaster 
#dimensions  : 5, 5, 2  (nrow, ncol, nlyr)
#resolution  : 72, 36  (x, y)
#extent      : -180, 180, -90, 90  (xmin, xmax, ymin, ymax)
#coord. ref. : lon/lat WGS 84 (CRS84) (OGC:CRS84) 
#source(s)   : memory
#names       :         A-1,        A-2 
#min values  : 0.003353635, 0.03063317 
#max values  : 0.926166175, 0.99872097 
#Warning message:
#In grep(i, names(x)) :
#  argument 'pattern' has length > 1 and only the first element will be used

Is there a way to implement this without using grep to write additional matching rules? It would be useful to simplify the code if multiple matches could be made in the same way as in the example.

The terra package supports a very convenient matching method for layer selection for multi-layer raster data:

# expression based (partial) matching of names with single brackets: s['^A']

However, this rule does not take effect for the time being when dealing with multiple conditions, as follows: I want to select all layers whose names start with 'A' and 'C':

library(terra)
data <- rast(nrow = 5, ncol =5, nlyr =6, vals = runif(5*5*6)) |> 
  `names<-`(c('A-1', 'A-2', 'B-1', 'B-2', 'C-1', 'C-2'))

data['^C'] #success

data[c('^A', '^C')]  
#> data[c('^A', '^C')]
#class       : SpatRaster 
#dimensions  : 5, 5, 2  (nrow, ncol, nlyr)
#resolution  : 72, 36  (x, y)
#extent      : -180, 180, -90, 90  (xmin, xmax, ymin, ymax)
#coord. ref. : lon/lat WGS 84 (CRS84) (OGC:CRS84) 
#source(s)   : memory
#names       :         A-1,        A-2 
#min values  : 0.003353635, 0.03063317 
#max values  : 0.926166175, 0.99872097 
#Warning message:
#In grep(i, names(x)) :
#  argument 'pattern' has length > 1 and only the first element will be used

Is there a way to implement this without using grep to write additional matching rules? It would be useful to simplify the code if multiple matches could be made in the same way as in the example.

Share Improve this question asked Mar 26 at 13:38 BreezeBreeze 3347 bronze badges 3
  • 2 You are not ok with subset(data, grepl('^A|C', names(data)))? Which could be extended by & filter2 or | filter3, where filter2/3 should return a boolean vector of same length. Btw, I do not know which pattern (maybe along '^A|C\\-\\d{1}$' might be appropriate for your actual case – Friede Commented Mar 26 at 13:46
  • Thanks, I think this one is a class of problem solving. But the main thing is that if I have a lot of matching conditions (only two conditions here), such that I need to write very complex grep rules out there. – Breeze Commented Mar 26 at 13:55
  • 1 Consider describing such a case in more detail. – Friede Commented Mar 26 at 16:44
Add a comment  | 

1 Answer 1

Reset to default 1

You can do

d <- data["^A|^C"]

d
#class       : SpatRaster 
#dimensions  : 5, 5, 4  (nrow, ncol, nlyr)
#resolution  : 72, 36  (x, y)
#extent      : -180, 180, -90, 90  (xmin, xmax, ymin, ymax)
#coord. ref. : lon/lat WGS 84 (CRS84) (OGC:CRS84) 
#source(s)   : memory
#names       :        A-1,       A-2,         C-1,        C-2 
#min values  : 0.08889804, 0.0016772, 0.000832798, 0.04425668 
#max values  : 0.97299366, 0.9975146, 0.947968749, 0.99642639 

That is, instead of a vector of conditions, concatenate them with the OR symbol.

发布评论

评论列表(0)

  1. 暂无评论