4

Is there a way to fill my NoData values in R?

I have a raster that has several nodata pixels, which looks like this: enter image description here

(each white dot has no data)

For the moment, the no data values are assigned -Inf:

Formal class 'RasterLayer' [package "raster"] with 12 slots
  ..@ file    :Formal class '.RasterFile' [package "raster"] with 13 slots
  .. .. ..@ name        : chr "D:\\Users\cost_based_on_density_new.asc"
  .. .. ..@ datanotation: chr "FLT4S"
  .. .. ..@ byteorder   : chr "little"
  .. .. ..@ nodatavalue : num -Inf
  .. .. ..@ NAchanged   : logi FALSE
  .. .. ..@ nbands      : int 1
  .. .. ..@ bandorder   : chr "BIL"
  .. .. ..@ offset      : int 0
  .. .. ..@ toptobottom : logi TRUE
  .. .. ..@ blockrows   : int 1
  .. .. ..@ blockcols   : int 892
  .. .. ..@ driver      : chr "gdal"
  .. .. ..@ open        : logi FALSE
  ..@ data    :Formal class '.SingleLayerData' [package "raster"] with 13 slots
  .. .. ..@ values    : logi(0) 
  .. .. ..@ offset    : num 0
  .. .. ..@ gain      : num 1
  .. .. ..@ inmemory  : logi FALSE
  .. .. ..@ fromdisk  : logi TRUE
  .. .. ..@ isfactor  : logi FALSE
  .. .. ..@ attributes: list()
  .. .. ..@ haveminmax: logi FALSE
  .. .. ..@ min       : num Inf
  .. .. ..@ max       : num -Inf
  .. .. ..@ band      : int 1
  .. .. ..@ unit      : chr ""
  .. .. ..@ names     : chr "cost_based_on_density_new"
  ..@ legend  :Formal class '.RasterLegend' [package "raster"] with 5 slots
  .. .. ..@ type      : chr(0) 
  .. .. ..@ values    : logi(0) 
  .. .. ..@ color     : logi(0) 
  .. .. ..@ names     : logi(0) 
  .. .. ..@ colortable: logi(0) 
  ..@ title   : chr(0) 
  ..@ extent  :Formal class 'Extent' [package "raster"] with 4 slots
  .. .. ..@ xmin: num -15.1
  .. .. ..@ xmax: num -7.64
  .. .. ..@ ymin: num 7.19
  .. .. ..@ ymax: num 12.7
  ..@ rotated : logi FALSE
  ..@ rotation:Formal class '.Rotation' [package "raster"] with 2 slots
  .. .. ..@ geotrans: num(0) 
  .. .. ..@ transfun:function ()  
  ..@ ncols   : int 892
  ..@ nrows   : int 658
  ..@ crs     :Formal class 'CRS' [package "sp"] with 1 slot
  .. .. ..@ projargs: chr NA
  ..@ history : list()
  ..@ z       : list()

How can I assign a new value (ex: 10000) to these nodata pixels in R?

  • 2
    Try x[!is.finite(x)] <- 10000 where x is the object your showed a print summary of. There's lots of other possibilities, but your question (IMO) needs to demonstrate that what you describe is actual, for this to work. Can you also try plot(x); points(xyFromCell(x, which(!is.finite(values(x)))), pch = "+") plot(is.finite(x)), to be sure? (That's what I'd be checking ...) – mdsumner Jan 26 '18 at 9:16
  • That worked it seems, the no data pixels are gone after x[!is.finite(x)] <- 10000; plot(x). If you want you could write an answer and I would check it – M514 Jan 26 '18 at 9:28
  • No worries, RobertH's answer is best :) – mdsumner Jan 26 '18 at 22:54
8

You are incorrect in assuming that "the no data values are assigned -Inf". (I understand that you get that idea from "@ nodatavalue : num -Inf", but that is not how it works when the driver is "gdal".)

The canonical way to change cells with NA to another value is

y <- reclassify(x, cbind(NA, 10000)) 

While it is possible to do

x[is.na(x)] <- 10000

I would generally discourage this approach as it is prone to memory limitation errors, and less flexible (e.g. you cannot provide a filename argument)

is.finite works too, because:

is.finite(NA)
#[1] FALSE

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.