[R-sig-Geo] Conditional operations and rasters

Vijay Lulla vijaylulla at gmail.com
Wed Sep 16 05:30:50 CEST 2015


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:
> 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
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo



More information about the R-sig-Geo mailing list