I would like to add a new column in a raster attribute table and populate it with values based on another column using if statement
, the new column should have letters such as A, B, C and D. How do I do that in R?
-
Is this the raster package with the ratify function? What have you tried?– mdsumnerOct 13, 2016 at 12:52
-
@mdsumner I used the ratify function but could not extract the attribute I created. I ended up coding the values based on another column.– jmutuaOct 14, 2016 at 11:10
1 Answer
Here a modified example from the documentation:
# create raster
r <- raster(nrow=10, ncol=10)
r[] = 1
r[51:100] = 2
r[3:6, 1:5] = 3
r[,1:2] = 4
# access raster attribute table
r <- ratify(r)
rat <- levels(r)[[1]]
# assign new letters attributes (A-D)
rat$letters <- LETTERS[1:4]
levels(r) <- rat
# get assigned letters for all raster cells
factorValues(r, extract(r, c(1:100)))$letters