I fitted a species distribution model and predicted the suitable habitat using this model, and then I got a map plotted in R.

I tried to save this prediction as a raster, using code:

rf<-writeRaster(px, filename="predict.tif", format="GTiff")

However, I could only export a tif format file without an attribute table. I could put this raster to ArcGIS software, but without an attribute table, it is difficult to run any analysis.

How can I export a raster with it's attribute table in R?

  • 1
    What do you mean by an attribute table? What are the values in your raster? The ratify function and things like as.factor might be what you want if you have a categorical raster and want to save labels for each class. – Spacedman Apr 14 at 14:23
  • I put the raster (.tif file) in ArcGIS, but there is no attribute table of that file, which make it difficult to do any analysis. The attribute table I expect should contain 1, 0 or 0~1 values of each cell. – Lili Li Apr 17 at 6:42
  • A raster with a single "attribute" will not have an attribute table because these are the values that encoded in the raster contains. You only really need an attribute table if you then have attributes associated with the raster values. In ArcGIS, if the raster is integer and not float, you can build an attribute table but, by default there will not be one. There is a tool in "Data Management toolbox > Raster toolset > Raster Properties toolset" to do this. – Jeffrey Evans Apr 19 at 14:33

I finally used rf<-writeRaster(px, filename="predict.tif", datatype="INT1U", format="GTiff", overwrite=TRUE) to export a integer type tiff file. And then I imported it in ArcGIS, shut down the background processing, and then I can create an attribute table for this raster layer using Arctool :)

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.