[R-sig-Geo] Antw: arithmetic with layers from a raster stack/brick

Matteo Mattiuzzi matteo.mattiuzzi at boku.ac.at
Thu Feb 28 21:43:10 CET 2013


Dear Ani,


Your "a" and "b" are not objects, they are the name the layers in your object "s". So you can't treat it that way.


Maybe this is what you want?


require(raster)
r<- raster(nrows=2, ncols=2)
r[]<-1:ncell(r)
s<-stack(r,r)
names(s)<-c("a","b")



r1<- s[["a"]] + s[["b"]]


Matteo 

>>> aniruddha ghosh  28.02.13 21.26 Uhr >>>
Dear List,

I'm trying to perform some arithmetic operation with layers of a raster
object. What I'm trying is

>require(raster)
>r<- raster(nrows=2, ncols=2)
>r[]<-1:ncell(r)
>s<-stack(r,r)
>names(s)<-c("a","b")

# now I want to add the layers of the raster "r" using their corresponding
names,i.e. "a" and "b" so that I get

>r1<-a+b

I don't want to use subset (or any other methods) to define "a" or "b".
This may be very trivial, but I couldn't do it.

Any suggestions?

Thanks
-- 
Ani

    [[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