[R-sig-Geo] raster: creating a layer of NA

Robert J. Hijmans r.hijmans at gmail.com
Thu May 26 08:12:29 CEST 2011


Hi Agus,

That happens because I defined Arith functions for Raster* objects and
numeric values, but not for Raster* objects and logical values. NA is
a logical value, see class(NA). I will add that.

Here are two more work arounds:

r <- D10N2009[[1]] * as.numeric(NA)

r <- calc(D10N2009[[1]], fun=function(x) x * NA)

Robert


On Wed, May 25, 2011 at 3:41 AM, Lyndon Estes <lestes at princeton.edu> wrote:
> Based on the example by Pierre:
>
> logo <- raster(system.file("external/rlogo.grd", package="raster"))
> # Use this rastre to create a new, void Raster object
> r <- raster(logo)
>
>> show(r)
> class       : RasterLayer
> dimensions  : 77, 101, 7777  (nrow, ncol, ncell)
> resolution  : 1, 1  (x, y)
> extent      : 0, 101, 0, 77  (xmin, xmax, ymin, ymax)
> projection  : +proj=merc +ellps=WGS84
> values      : none
>
> # Set to NA
> r[] <- NA
>> show(r)
> class       : RasterLayer
> dimensions  : 77, 101, 7777  (nrow, ncol, ncell)
> resolution  : 1, 1  (x, y)
> extent      : 0, 101, 0, 77  (xmin, xmax, ymin, ymax)
> projection  : +proj=merc +ellps=WGS84
> values      : in memory
> min value   : NA
> max value   : NA
>
> So this should work for your raster:
>
> D10N2009[] <- NA
>
>
> Cheers, Lyndon
>
>
> On Wed, May 25, 2011 at 2:32 AM, Roman Luštrik <roman.lustrik at gmail.com> wrote:
>> I would use setValues on individual layer.
>>
>> Cheers,
>> Roman
>>
>>
>>
>> On Wed, May 25, 2011 at 8:25 AM, Pierre Roudier <pierre.roudier at gmail.com>wrote:
>>
>>> Hi Agustin,
>>>
>>> What about this:
>>>
>>> # Create a Raster object
>>> logo <- raster(system.file("external/rlogo.grd", package="raster"))
>>> # Use this rastre to create a new, void Raster object
>>> r <- raster(logo)
>>> show(r)
>>>
>>> Pierre
>>>
>>> 2011/5/25 Agustin Lobo <alobolistas at gmail.com>:
>>> > HI!
>>> >
>>> > I must create a layer with all missing values. I try:
>>> >> show(D10N2009)
>>> > class       : RasterBrick
>>> > dimensions  : 393, 606, 35  (nrow, ncol, nlayers)
>>> > resolution  : 0.008928571, 0.008928571  (x, y)
>>> > extent      : -2.004464, 3.40625, 40.49554, 44.00446  (xmin, xmax, ymin,
>>> ymax)
>>> > projection  : +proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs
>>> +towgs84=0,0,0
>>> > values      :
>>> /media/Iomega_HDD/FLUXPYR/VGTFLUXPYR1/D10/FLXP_NDD10_2009.vrt
>>> > min values  : 0 0 0 0 0 0 0 0 0 0 ...
>>> > max values  : 255 255 255 255 255 255 255 255 255 255 ...
>>> >
>>> >> delme = D10N2009[[1]]*0
>>> >> show(delme)
>>> > class       : RasterLayer
>>> > dimensions  : 393, 606, 238158  (nrow, ncol, ncell)
>>> > resolution  : 0.008928571, 0.008928571  (x, y)
>>> > extent      : -2.004464, 3.40625, 40.49554, 44.00446  (xmin, xmax, ymin,
>>> ymax)
>>> > projection  : +proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs
>>> +towgs84=0,0,0
>>> > values      : in memory
>>> > min value   : 0
>>> > max value   : 0
>>> >
>>> > But
>>> >> delme = D10N2009[[1]]*NA
>>> > Error in D10N2009[[1]] * NA : non-numeric argument to binary operator
>>> >
>>> > I also try
>>> >> NAvalue(delme) <- 0
>>> > but no trace of the NAs:
>>> >> show(delme)
>>> > class       : RasterLayer
>>> > dimensions  : 393, 606, 238158  (nrow, ncol, ncell)
>>> > resolution  : 0.008928571, 0.008928571  (x, y)
>>> > extent      : -2.004464, 3.40625, 40.49554, 44.00446  (xmin, xmax, ymin,
>>> ymax)
>>> > projection  : +proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs
>>> +towgs84=0,0,0
>>> > values      : in memory
>>> > min value   : 0
>>> > max value   : 0
>>> >
>>> > By now I write with NAflag=0 and read back, but there must be a
>>> > cleaner way of generating a layer with all NA values
>>> > (it's a missing layer for a particular time)
>>> >
>>> > Thanks!
>>> >
>>> > Agus
>>> >
>>> > _______________________________________________
>>> > R-sig-Geo mailing list
>>> > R-sig-Geo at r-project.org
>>> > https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>>> >
>>>
>>>
>>>
>>> --
>>> Scientist
>>> Landcare Research, New Zealand
>>>
>>> _______________________________________________
>>> R-sig-Geo mailing list
>>> R-sig-Geo at r-project.org
>>> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>>>
>>
>>
>>
>> --
>> In God we trust, all others bring data.
>>
>>        [[alternative HTML version deleted]]
>>
>> _______________________________________________
>> R-sig-Geo mailing list
>> R-sig-Geo at r-project.org
>> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>>
>
>
>
> --
> Lyndon Estes
> Research Associate
> Woodrow Wilson School
> Princeton University
> +1-609-258-2392 (o)
> +1-609-258-6082 (f)
> +1-202-431-0496 (m)
> lestes at princeton.edu
>



More information about the R-sig-Geo mailing list