I have data with population counts in GeoTIFF format and would like to convert it to a data.frame using R.

The data I have can be accessed here.

I would like to obtain a table with the form:

longitude latitude value
-180 -90 0
-180 -89 0
-180 -88 0
-180 -87 0

So far I could obtain the values using raster::getValues, but the coordinate system is messed up.

  • By "Messed up" what do you mean? Some of the data from that link seems to be not in lat-long projection, so maybe you are seeing those projected coordinates. Can you please edit to say which file from the download repository (jeodpp.jrc.ec.europa.eu/ftp/jrc-opendata/GHSL/…) you are using.
    – Spacedman
    Mar 25, 2023 at 21:20
  • @Spacedman edited link to the dataset and table with an example of how I intend to have the data.
    – Santiago
    Mar 26, 2023 at 4:24
  • I'm still not sure what "messed up" means. getValues doesn't show the coordinates at all, so do you mean you can't see the coordinates? Does as.data.frame work as in the answer given below? If not, comment on that answer with why.
    – Spacedman
    Mar 26, 2023 at 8:14
  • Another problem with that specific raster is that it is 18000 by 36082 pixels, so any data frame would be 650 million rows and R would likely struggle to work with it.
    – Spacedman
    Mar 26, 2023 at 11:02

1 Answer 1

library (terra)

r = rast("your_raster.tif")
df = as.data.frame(r, xy = TRUE, na.rm = TRUE) # na.rm = TRUE if you want to exclude the NA values from your df

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