4

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?

2
  • Is this the raster package with the ratify function? What have you tried?
    – mdsumner
    Oct 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.
    – jmutua
    Oct 14, 2016 at 11:10

1 Answer 1

2

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.