[R-sig-Geo] overlay() in raster package
Wearn, Oliver
oliver.wearn08 at imperial.ac.uk
Thu Feb 25 18:40:15 CET 2010
Thanks Robert, that makes sense. I guess the function is working on the rasters on a row-by-row basis, rather than cell-by-cell basis.
Many thanks,
Oliver
________________________________________
From: Robert J. Hijmans [r.hijmans at gmail.com]
Sent: 25 February 2010 17:12
To: Wearn, Oliver
Cc: r-sig-geo at stat.math.ethz.ch
Subject: Re: [R-sig-Geo] overlay() in raster package
Oliver,
The function passed to overlay should be work if the arguments are
vectors; but your function only works if the arguments are single
numbers.
Rather than this:
fun = function(x,y) {
if (is.na(x)) {
if (y==1) { return(0)
} else { return(x) }
} else { return(x) }
}
Do this:
fun = function(x,y) {
x[is.na(x) & y==1] <- 0
return(x)
}
Robert
On Thu, Feb 25, 2010 at 7:23 AM, Wearn, Oliver
<oliver.wearn08 at imperial.ac.uk> wrote:
> Thanks Kamran, unfortunately I don't think the placement of 'else' has any effect - the same errors arise.
>
> To answer the request for more details, here's a self-contained version of what my data pretty much looks like:
>
> require(raster)
> rs <- raster(system.file("external/test.grd", package="raster"))
> ENN.500<-reclass(rs,rclmat=matrix(c(200,250,NA),ncol=3,byrow=T))
> forest.cover<-reclass(rs,rclmat=matrix(c(0,160,0,160,290,1,290,xmax(rs),0),ncol=3,byrow=T))
> plot(stack(forest.cover,ENN.500))
>
> Cheers,
> Oliver
>
> ________________________________________
> From: Kamran Safi Radolfzell [ksafi at mail.orn.mpg.de]
> Sent: 25 February 2010 14:42
> To: Wearn, Oliver; r-sig-geo at stat.math.ethz.ch
> Cc: r-sig-geo-bounces at stat.math.ethz.ch
> Subject: Re: [R-sig-Geo] overlay() in raster package
>
> Hi Oliver,
>
> don't really understand what you are trying to do with the overlay, but the
> "if" issue might be due to the wrong use of your "else".
>
> if(...)
> {
> ...
> }else{ #important to have else on the same line
> ...
> }
>
> This might help.
> If you need more help on the overlay, I guess more details would be helpful.
>
> hth
>
> Kami
>
> On Thu, 25 Feb 2010 14:34:51 +0000, Wearn, Oliver wrote
>> Dear all,
>>
>> I'm trying to use the overlay() function in the raster package, but
>> can't get 'if' clauses to work. Can anyone help?
>>
>> I have two rasters, the first of which I would like to modify,
>> depending on the value in the second (a forest cover raster).
>> Specifically, if the value in the first (called ENN.500) is NA, I
>> would like to change the value to zero, but only if the value in the
>> forest cover raster is 1 (otherwise the value should just be left).
>>
>> So, I tried:
>>
>> test <- overlay(x = ENN.500,y = forest.cover,fun = function(x,y) {
>> if (is.na(x)) {
>> if (y==1) {
>> return(0)
>> }
>> else {
>> return(x)
>> }
>> }
>> else {
>> return(x)
>> }
>> })
>>
>> ...but I'm guessing overlay() doesn't work like this? I'm getting
>> the following errors:
>>
>> Error: object 'applymethod' not found
>> In addition: Warning message:
>> In if (is.na(x)) { :
>> the condition has length > 1 and only the first element will be used
>>
>> I also tried:
>>
>> test <- overlay(x = ENN.500,y = forest.cover,fun = function(x,y) {
>> if (x[is.na(x)]) {
>> if (y[y==1]) {
>> return(0)
>> }
>> else {
>> return(x)
>> }
>> }
>> else {
>> return(x)
>> }
>> })
>>
>> ...which returned the following error:
>>
>> Error in .overlayList(rasters, fun = fun, filename = filename,
>> datatype = datatype, : cannot use this formula
>>
>> The alternative would be to use arithmetic operators:
>>
>> ENN.500[is.na(ENN.500] <- 0
>> forest.cover[forest.cover==0] <- 1
>> result <- ENN.500*forest.cover
>>
>> ...but it says in ?overlay that this is slow for very large rasters
>> (which I will be using), so I'm guessing I should try not to use
>> this method?
>>
>> Thanks, in advance, for any help whatsoever.
>> Oliver
>> _______________________________________________
>> R-sig-Geo mailing list
>> R-sig-Geo at stat.math.ethz.ch
>> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>
>
> --
> Open WebMail Project (http://openwebmail.org)
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at stat.math.ethz.ch
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>
More information about the R-sig-Geo
mailing list