Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

specifying labels using cut() on raster

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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?

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading