1

I am using the package raster to work with GeoTIFF raster files which I exported from GRASS using r.out.gdal. GRASS assigned the value -nan to some raster files (type FLOAT32) for missing values. Other files had 65535 or 255 assigned to missing values.

In R, I reassign these values to missing like this:

raster[raster==255] <- NA

The problem is that I do not know what to do with multiple rasters that had the -nan assigned to missing values. For what I see when I examine the structure of these rasters (which I have as part of a list), the -nan are stored as NaN.I tried doing:

rasterlist[["element_i"]][rasterlist[["element_i"]] == NaN] <- NA

but I do not see any difference, for example when I do:

getValues(rasterlist[["element_i"]], row=1000)

The output is something like:

8100 NaN 8101 NaN NaN 8102 8104 8106

so the NaN are not transformed into NA.

Any suggestion?

2 Answers 2

1

A NaN is different than NA. The NaN often results from a divide by zero error whereas NA is the R value for no data. These values behave in specific ways and it would be good for you to read some R background material to understand the behavior. Two useful operators to be aware of are: is.na() and is.nan().

y=c(0,1,2,3,4,NA)
x=c(0,1,2,3,4,NA)
(d=x/y)

is.na(d)
is.nan(d)

d[is.nan(d)] <- NA 
d

( d = c(NaN, 1:10, NA) )
d[d <= 5 | is.nan(d)] <- NA
d

Please keep in mind that the way you are vectorizing the problem is causing the entire raster to be read into memory. To keep the problem memory safe pass a function, setting NA's and replacing NaN's, to calc.

8
  • I was going to pass a function but I have the following problem. My raster for elevation may have the value 255. When I write a function like: raster_na <- function(rast) { for (i in 1:length(raster.list){ rast[[vars]][rast[[vars]]== c(255,65535)] <- NA } return(rast) } I may convert an elevation value into NA, right?
    – MercedesRD
    Feb 18, 2015 at 15:56
  • Your function cannot be passed to calc. You need something that is vectorized like the example that I provided. Take a look at the help for calc. Feb 18, 2015 at 16:01
  • So if I just say, for example raster[raster == 65535] <- NA that would work?
    – MercedesRD
    Feb 18, 2015 at 16:14
  • I think I got it. fun_na = function(raster){raster[raster==255] <- NA}
    – MercedesRD
    Feb 18, 2015 at 16:30
  • 1
    Almost. Something like this will set 255, 65535 and NaN values to NA: calc(r, function(x) {x[x == 255 | x == 65535 | is.nan(x)] <- NA; return(x) }) If you read the help for calc you will see that, using the filename argument, you can specify a raster that is read to disk. If the raster is large and you define an output raster, the function will automatically read the data in blocks to keep it memory safe. Feb 18, 2015 at 16:32
0

A pesky feature of rasters is the multitude of "no data" possibilities, and how software doesn't consistently deal with it. What I expect that GRASS is doing is assigning minus infinity to the missing values.

While not the best of options, you could try to go about it the other way around, eg. identify all acceptable values and then assign NA to the ones not accepted.
In R I'd do it like this:

raster[!(raster<255 & raster>=0)] <- NA

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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