I'm a bit confused about types here. While I'm loading a image:

tif = raster(paste0(path))

I get some "Formal Class RasterLayer" object. But while I change it's matrix of values, for example:

idx <- values(tif) != 200
  values(tif)[idx] <- 255

if becomes "Large RasterLayer" object. And indeed it's large, for file of about 100Mpx it's over 270mb, while that first is about 10mb in memory.

How can I convert this new one into the "Formal" class to save memory?

I tried: tif1 = raster(tif) but object missing coordinates. as.raster(tif) returns raster chr variable.


Found a workaround:

writeRaster(tif, "tif.tif")
tif = raster("tif.tif")

It takes much memory for process but at last its freed. Wish to find something faster without saving to disk.

  • Can you explain where you get those sizes from? I have a 1200x942 =~ 1Mpx file and the source tif is 63k bytes long and the R object is 16k bytes big according to object.size(tif).
    – Spacedman
    Jun 14, 2019 at 14:18
  • Sorry, my mistake -> 100Mpx, not 1Mpx. I read it from RStudio Global Env. view and it's quite what is memory showing independently.
    – Peter.k
    Jun 14, 2019 at 20:31


Your Answer

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

Browse other questions tagged or ask your own question.