Home > Blockchain >  specifying labels using cut() on raster
specifying labels using cut() on raster

Time:05-20

Suppose I have a the following raster:

library(raster)    
r <- raster(ncols=36, nrows=18)
values(r) <- rnorm(ncell(r)) 

I can discretise its values using cut():

breaks <- -2:2 * 3
rc <- cut(r, breaks=breaks)

However, when i try to specify labels e.g., A, B, C, etc

rc <- cut(r, breaks=breaks, labels = c("A", "B", "C", "D", "E"))

it returns the following error.

Error in cut.default(getValues(x), breaks = breaks, labels = FALSE,
...) :    formal argument "labels" matched by multiple actual
arguments

I tried googling the error message but no luck. Any ideas what is going wrong?

CodePudding user response:

Firstly, you only have 5 breaks, so you can only have 4 labels. Secondly, you could use cut on the data of the raster rather than the whole raster object:

rc <- r
rc[] <- cut(rc[], breaks = breaks, labels = c("A", "B","C","D"))
rc
#> class      : RasterLayer 
#> dimensions : 18, 36, 648  (nrow, ncol, ncell)
#> resolution : 10, 10  (x, y)
#> extent     : -180, 180, -90, 90  (xmin, xmax, ymin, ymax)
#> crs        :  proj=longlat  datum=WGS84 
#> source     : memory
#> names      : layer 
#> values     : 1, 4  (min, max)
#> attributes :
#>  ID VALUE
#>   1     A
#>   2     B
#>   3     C
#>   4     D
  • Related