Skip to content

Conditional operations and rasters

5 messages · Thiago V. dos Santos, Vijay Lulla, Andrew Vitale +1 more

#
Hi all,

This is probably a silly question, but I could not find an appropriate answer somewhere else.

I am trying create a raster based on conditional statements applied on an original raster.

This is some sample code:

lai <- raster(ncols=360, nrows=180)
lai[] <- 1:ncell(lai)

if (lai < 1000) {

    emiss_0  <- 0.95 + (0.01 * lai)
    emiss_nb <- 0.97 + (0.0033 * lai)


} else if (lai >= 1000) {


    emiss_0  <- 0.95
    emiss_nb <- 0.98


}

Error in if (lai < 3) { : argument is not interpretable as logical


What would be the right way to do that? 
 Greetings,
 -- Thiago V. dos Santos

PhD student
Land and Atmospheric Science
University of Minnesota
#
I have no idea what you're trying to accomplish with your program
logic but I think the below might work for you.

R> lai <- raster(ncols=360,nros=180)
R> lai[] <- 1:ncell(lai)

R> idx <- lai < 1000
R> emiss_0 <- lai
R> emiss_0[] <- 0.95
R> emiss_0[idx] <- 0.95 + (lai[idx]*0.01)

R> emiss_nb <- lai
R> emiss_nb[] <- 0.98
R> emiss_nb[!idx] <- 0.97+(lai[!idx]*0.0033)
R> ?Which # from raster package

Other gurus on the list might suggest more efficient way[s] of doing this.

HTH,
Vijay.

On Tue, Sep 15, 2015 at 10:51 PM, Thiago V. dos Santos
<thi_veloso at yahoo.com.br> wrote:
#
I would do something like the following:

library(raster)

lai <- raster(ncols=50, nrows=50)
lai[] <- 1:ncell(lai)

emmiss_0_func <- function(lai) {
    emmiss_0 <- ifelse((lai < 1000), (0.95 + (0.01 * lai)), 0.95)
    return(emmiss_0)
}
emmiss_nb_func <- function(lai) {
    emmiss_nb <- ifelse((lai < 1000), (0.97 + (0.0033 * lai)), 0.98)
    return(emmiss_nb)
}

emiss <- stack(calc(lai, emmiss_0_func), calc(lai, emmiss_nb_func))
plot(emiss)
On Tue, Sep 15, 2015 at 8:30 PM, Vijay Lulla <vijaylulla at gmail.com> wrote:

            

  
    
3 days later
#
You can also try this:

f <- function(lai) {
    emiss_0  <- 0.95 + (0.01 * lai)
    emiss_nb <- 0.97 + (0.0033 * lai)
    i <- lai >= 1000
    emiss_0[i]  <- 0.95
    emiss_nb[i] <- 0.98
    cbind(emiss_0, emiss_nb)
}

library(raster)
rlai <- raster(ncols=360, nrows=180)
rlai[] <- 1:ncell(rlai)
x <- calc(rlai, f)
x
On Tue, Sep 15, 2015 at 8:47 PM, Andrew Vitale <vitale232 at gmail.com> wrote:
3 days later
#
Thank you all for the suggestions, Robert, Andrew and Vijay.

I am still examining all of them to see which one works best for me.
 Greetings,
 -- Thiago V. dos Santos

PhD student
Land and Atmospheric Science
University of Minnesota
On Saturday, September 19, 2015 8:15 PM, Robert J. Hijmans <r.hijmans at gmail.com> wrote:
You can also try this:

f <- function(lai) {
    emiss_0  <- 0.95 + (0.01 * lai)
    emiss_nb <- 0.97 + (0.0033 * lai)
    i <- lai >= 1000
    emiss_0[i]  <- 0.95
    emiss_nb[i] <- 0.98
    cbind(emiss_0, emiss_nb)
}

library(raster)
rlai <- raster(ncols=360, nrows=180)
rlai[] <- 1:ncell(rlai)
x <- calc(rlai, f)
x
On Tue, Sep 15, 2015 at 8:47 PM, Andrew Vitale <vitale232 at gmail.com> wrote: