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.
Chelmy88
  • 164
  • 8
quoc-non duong
  • 51
  • 1
  • 1
  • 2

2 Answers2

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)
aldo_tapia
  • 13,560
  • 5
  • 30
  • 56
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.

Chelmy88
  • 164
  • 8
  • Thank Chelmy88 so much. Please give me some details about "through a loop" in R (functions or commands, advanced packages....). – quoc-non duong Aug 19 '19 at 01: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 Aug 19 '19 at 07: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. – quoc-non duong Aug 19 '19 at 08: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)

    – quoc-non duong Aug 19 '19 at 08: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 Aug 19 '19 at 09:05
  • Thank Chelmy88, I can run. Just add "full.name=TRUE" in input=list.files("path_to_data",pattern="*.tif$",full.name=TRUE). Then # raster::stack(input). – quoc-non duong Aug 19 '19 at 14:52