Search code examples
rfiledirectoryrasteroperation

How to perform operations between raster stacks and repeat the operations to all the folders in R?


I am a new user of R. My questions is closed to a recent asked questions on stack overflow website:How to stack individual raster layers from files contained in individual subfolders in R? . I used the answers provided for my own case. I have several folders containing several raster files. The description of the files is the same for each folder. To simplify, I have two folders A and B containing each 16 rasters files. Either folder A or B, raster files have the same type of name so they can be identified with patterns.

                        |----8 raster files pattern "snow"
              Folder A- |      
              |         |----8 raster files pattern "rain"
parent_folder-|
              |        |----8 raster files pattern "snow"
              Folder B-|      
                       |----8 raster files pattern "rain"

I used the code provided in the last question (see link):

list_dirs <- list.dirs("path/parentfolder/", recursive = F)

names(list_dirs) <- basename(list_dirs)


# create two stacks with the pattern "snow" for the folders A and B, respectively

raster.list.snow <- lapply(list_dirs, function(dir) {
stack(list.files(dir, pattern = "snow", full.names = T, recursive = F))
})

# create two stacks with the pattern "rain" for the folders A and B, respectively

raster.list.rain <- lapply(list_dirs, function(dir) {
stack(list.files(dir, pattern = "rain", full.names = T, recursive = F))
})

I would like to perform calculis within each folder. For example, I would like in the folder A to multiply raster stack with the pattern "snow" with the raster stack with the pattern '"rain", and that doing the same thing for the folder B.

I tried the following code:

   raster.multiply<- raster.list.snow * raster.list.rain

I should have 8 new raster files for each folder A and B.

Then I would like to write in each folder the files obtained from raster.multiply.

However, for the last line of code, I got the following error:

 Error in raster.list.snow * raster.list.rain : 
 non-numeric argument to binary operator

I am very new in R and I am still exploring.


Solution

  • yuo wont be able to multiply the resulting lists of rasters together like that, hence the error (go investigate lists).

    but you can easily stack lists of stacks:

    # make some dummy rasters
    a <- raster(xmn=0,xmx=5,ymn=0,ymx=5,res=1)
    a[] <- sample(1:5,25,replace=T)
    b <- raster(xmn=0,xmx=5,ymn=0,ymx=5,res=1)
    b[] <- sample(1:5,25,replace=T)
    c <- raster(xmn=0,xmx=5,ymn=0,ymx=5,res=1)
    c[] <- sample(1:5,25,replace=T)
    d <- raster(xmn=0,xmx=5,ymn=0,ymx=5,res=1)
    d[] <- sample(1:5,25,replace=T)
    
    # imagine st1 and st2 are rain rasters, st3 and st4 are snow
    st1 <- stack(a,b,c,d)
    st2 <- stack(d,a,c,b)
    st3 <- stack(c,b,a,d)
    st4 <- stack(a,d,b,c)
    
    # make the rain and snow lists, just like your code above. we have a list object of two stacks.
    list1 <- list(st1,st2)
    list2 <- list(st3,st4)
    
    # error below
    list1 * list2
    Error in list1 * list2 : non-numeric argument to binary operator
    
    # however, stack them and multiply:
    stack(list1) * stack(list2)
    
    # check e.g.
    identical(getValues(stack(list1)[[1]]),getValues(a))
    [1] TRUE