1

I have two Large Rasterstack (6000 each ). I would like to do some simple calculations such as

         NewRasterStack = RasterStack_1 + RasterStack2*(-3)

Could you be kind to help me to suggest working code to do this calculation ? All the data is processed using R raster package, so I am looking for raster calc function.

Thank you in advance!

WAW
  • 143
  • 1
  • 10
  • It would be great if you could supply a minimal reproducible example to go along with your question. Something we can work from and use to show you how it might be possible to answer your question. That way others can also befit form your question, and the accompanying answer, in the future. You can have a look at [this SO post](http://stackoverflow.com/help/mcve) on how to make a great reproducible example in R. – Eric Fail Jan 15 '16 at 18:09
  • 2
    Have you tried doing `NewRasterStack <- RasterStack_1 + RasterStack2*(-3)`? It should just work, if both stacks have the same dimensions, number of layers, etc. – Josh O'Brien Jan 15 '16 at 18:10
  • For additional context to @JoshO'Brien comment: [Difference between = and <-](http://stackoverflow.com/questions/2271575/whats-the-difference-between-and-in-r) – Badger Jan 15 '16 at 18:56
  • 1
    @Badger Actually, `=` will work here just as well as `<-`. I probably should have said "Try doing `NewRasterStack = RasterStack_1 + RasterStack2*(-3)`". The key point here is that the **raster** package overloads both the `+` and `*` operators (along with others) so that they "just work" as you might hope they would, when passed pairs of `Raster*` objects. – Josh O'Brien Jan 15 '16 at 19:02
  • Aha! Thanks @JoshO'Brien, I'll be careful with my assumptions in the future. – Badger Jan 15 '16 at 20:01
  • 1
    @jbaums, true. Very true. I'll update it before I pour from the can the next time. – Eric Fail Jan 16 '16 at 00:21

1 Answers1

1

If the stacks have the same extent and resolution this should work:

NewRasterStack = RasterStack_1 + RasterStack2*(-3)

This might be bit more efficient:

nr <- overlay(RasterStack_1, RasterStack2, fun=function(x,y) x - 3 * y)
Robert Hijmans
  • 40,301
  • 4
  • 55
  • 63