5

I have 365 rasters (daily_sm2rain data of a year) in .TIF. I would like to create a new raster which is sum of 365 input rasters in R (~ sum of rain in a year). How can I do this?

I tried input=list.files("path_to_data",pattern="*.tif$"). it's ok.

map <- brick(paste0("path_to_data",input[1])) fives:

Error is "Error in .local(.Object, ...) : Error in .rasterObjectFromFile(x, objecttype = "RasterBrick", ...) : Cannot create a RasterLayer object from this file. (file does not exist)

print(input)
    [1] "daily.rainfall_2018-01-02_10km.tif" "daily.rainfall_2018-01-03_10km.tif"
      [3] "daily.rainfall_2018-01-04_10km.tif" "daily.rainfall_2018-01-05_10km.tif"
      [5] "daily.rainfall_2018-01-06_10km.tif" "daily.rainfall_2018-01-07_10km.tif"
      [7] "daily.rainfall_2018-01-08_10km.tif" "daily.rainfall_2018-01-09_10km.tif"
      [9] "daily.rainfall_2018-01-10_10km.tif" "daily.rainfall_2018-01-11_10km.tif"
     [11] "daily.rainfall_2018-01-12_10km.tif" "daily.rainfall_2018-01-13_10km.tif"
     [13] "daily.rainfall_2018-01-14_10km.tif" "daily.rainfall_2018-01-15_10km.tif
....
361 rows.
4
  • Whats the final goal, plot them of writing them to a file?
    – Chelmy88
    Commented Aug 17, 2019 at 10:07
  • 1
    Do you know how to read in one raster file? Please show some code so we know where to start.
    – Spacedman
    Commented Aug 17, 2019 at 12:56
  • See my answere. Does it work for you?
    – Chelmy88
    Commented Aug 17, 2019 at 23:44
  • What do you obtain by just running paste0("path_to_data",input[1])?
    – Chelmy88
    Commented Aug 19, 2019 at 11:07

2 Answers 2

9

So close, use:

files = list.files("path_to_data",pattern="*.tif$", full.names=TRUE)

rs <- brick(files)

or

rs <- stack(files)

Don't use map as name, there're several R functions with the same name.

Then, to create a sum of all your rasters use calc function (from raster package):

rs1 <- calc(rs, sum)
2
  • Thanks aldo_tapia so much. It worked. Commented Aug 19, 2019 at 14:49
  • You're welcome. You can accept the answer if you want, closing this topic
    – aldo_tapia
    Commented Aug 19, 2019 at 15:03
2

You can read the raster in R with the rasterpackage.

It can be loaded with map <- brick("file_path"). Then you can apply addition operation between your loaded objects. If you have 365 of them, you should read them through a loop.

6
  • Thank Chelmy88 so much. Please give me some details about "through a loop" in R (functions or commands, advanced packages....). Commented Aug 19, 2019 at 1:28
  • What is your knowledge in R? I can show you an example, I would need to know how your files are called then
    – Chelmy88
    Commented Aug 19, 2019 at 7:22
  • Hi Chelmy88, I am quite new to R. I tried rasterstack [# input=list.flies("path_to_data",pattern="*.tif$") # stack=raster::stack(input)]. But it didn't work. Commented Aug 19, 2019 at 8:42
  • Error in .local(.Object, ...) : Error in .rasterObjectFromFile(x, band = band, objecttype = "RasterLayer", : Cannot create a RasterLayer object from this file. (file does not exist) Commented Aug 19, 2019 at 8:48
  • Should be list.files and not list.flies, then try map <- brick(paste0("path_to_data",input[1])). What do you get. And what do you get with print(input)after running list.files (do not hesitate to add it to your question, it is more readable than in comments).
    – Chelmy88
    Commented Aug 19, 2019 at 9:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.