[R-sig-Geo] Conditional operations and rasters
Robert J. Hijmans
r.hijmans at gmail.com
Sun Sep 20 03:15:43 CEST 2015
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:
> 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:
>
>> 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
>>
>> _______________________________________________
>> R-sig-Geo mailing list
>> R-sig-Geo at r-project.org
>> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>>
>
>
>
> --
> *Andrew P. Vitale*
> Staff Research Scientist
> Desert Research Institute
> 2215 Raggio Pkwy
> Reno, NV 89512
>
> [[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
More information about the R-sig-Geo
mailing list