Ezra
Ezra

Reputation: 169

R: How to combine rasters in a loop with all possible combinations?

I have three raster in a list.

rasterlist <- (r1, r2, r3)

I have one operation to combine each raster with another - let's say add them.

How do I write a loop which combines all the rasters iteratively?

Like this:

result1 <- r1+r2 
result2 <- r2+r3
result3 <- r1+r3

Note: My operation within {}is around 200 lines long and not a simple addition, which is why I need a nice loop around.

Upvotes: 1

Views: 289

Answers (1)

akrun
akrun

Reputation: 887691

We can use combn to get the combination of list element and sum them

combn(rasterlist, 2, FUN = function(x) x[1] + x[2])

Upvotes: 2

Related Questions