Search code examples
rdataframerasterterra

Error when converting large Formal class SpatRaster to dataframe


I am attempting to convert a large Formal class SpatRaster to a dataframe, but receive an error, that seems to be related to memory allocation but I am not sure.

#Libraries:

library(terra)
library(raster)
library(geodata)

#Assigning the extent of the world that I am analyzing (approximately the Americas):

extent_obj <- extent(x = c(-160, -34, -46, 50))

#Pulling in the data from the internet (careful, for this can take up to 4 hours, only here for maximum reproducibility):

bioclim_all <- worldclim_global(var = "bio",
                                res = 0.5,
                                path = "data/")

#Crop the data to the extent:

crop_bioclim_all <- crop(x = bioclim_all, y = extent)

#Attempt to convert this object to a dataframe:

data_frame_bio_all <- terra::as.data.frame(crop_bioclim_all)

However, I receive the following error:

Error in x@ptr$readValues(row - 1, nrows, col - 1, ncols) : 
  std::bad_alloc

Does anyone know what might lead to this error? I ended up manually downloading the data and using this same code to read it (getData first checks the working directory for the data described before attempting to download), and the file is quite large, around 10 GB of data. For reference, this code runs in less than 2 minutes on my machine when working with the much lower resolution file from the same website, the res = 10 data.


Solution

  • You have about 17 million values

    library(terra)
    r <- rast(res=1/120 , ext=c(-160, -34, -46, 50), nlyr=19)
    ncell(r) / 10e6
    #[1] 17.41824
    

    Your computer cannot load all these values into memory, at least not in one step in which case it needs contiguous memory. You could try to read layer by layer.

    But why are you going down this dark path? There hardly ever is a good reason for making a data.frame like this. It suggests you are not very familiar with working with raster that and that you need to ask another question first (about your goal).