0

Forgive me as I am very new to coding and using r. I am trying to import many .tif files into rstudio that I obtained from WorldClim (historical monthly weather data 2010-2018). There are 120 .tif files in a zipped folder which I have unzipped. I tried to import them into a rasterbrick but it looks like it is just one raster layer

brk1 <- do.call(brick, lapply(list.files(path = 
         "/Classes F2020/ES 232/tmin_tmax/wc.2.1_2.5m_tmax_2010-2018", 
         pattern = "Band*.*tif"), raster))
View(brk1)
brk

#class      : RasterBrick 
#dimensions : 180, 360, 64800, 1  (nrow, ncol, ncell, layers)
#resolution : 1, 1  (x, y)
#extent     : -180, 180, -90, 90  (xmin, xmax, ymin, ymax)
#crs        : +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0 
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213

2 Answers2

0

There are a number of ways to do this, but here is a simple method. It's not the most efficient, but I think it's pretty straightforward for a new user to implement:

rb<-brick() #create an empty brick

for(i in 1:length(list.files)){
  tif.name<- list.files(pattern="*tif$", full.names=F, recursive=FALSE)
  print(tif.name) #check name of tif file
  tif<-raster(tif.name) #assign tif file to raster object
  tif.c<-tif*mask.AOI #crop layer to AOI; mask.AOI is a raster where 1 = your study area, NA is all other values

  rb<-addLayer(rb, tif.c) #add cropped tif file to brick
}
Tim Assal
  • 677
  • 6
  • 15
  • Thank you so much Tim. I tried that. I have 107 .tif files that is in my directory in r. I tried putting in the name of that file but of course it did not work – desertbee Apr 17 '20 at 16:56
  • > for(i in 1:length(list.files)){ + tif.name<- list.files(pattern="*tif$", full.names=F, recursive=FALSE) + print(wc.2.1_2.5m_tmax_2010-2018) + tif<-raster(tif.tmin) #assign tif file to raster object + rb<-addLayer(rb, wc.2.1_2.5m_tmax_2010-2018) #add tif file to brick + } Error in print(wc.2.1_2.5m_tmax_2010 - 2018) : object 'wc.2.1_2.5m_tmax_2010' not found – desertbee Apr 17 '20 at 16:56
0

In your call to lapply you use raster and that will give you only one layer. I think what you are looking for is something along these lines (and it always better to do things step by step, so that you can inspect what is going on along the way)

library(raster)
f <- list.files(pattern = "tif$")
x <- lapply(f, brick)
s <- stack(x)
Robert Hijmans
  • 40,301
  • 4
  • 55
  • 63
  • Thank you so much Robert! Your code worked up to s. I got an error saying "different extent" ...I don't see how that is possible. f <- list.files(pattern = "tif$") > x <- lapply(f, brick) > s <- stack(x) Error in compareRaster(rasters) : different extent – desertbee Apr 17 '20 at 16:59
  • All rasters in a stack/brick need to be the same extent. The extent of the layers are likely not consistent if you received this message. I often add a step to crop the layer to my area of interest to ensure this is not a problem and save space. – Tim Assal Apr 17 '20 at 17:13
  • I've added a step in my code above to crop the raster before it's added to the stack. I don't know that you can do that with Robert's code as is, but if there is a way, he will know! – Tim Assal Apr 17 '20 at 17:19
  • But I have 240 .tif files. 120 each in two zipped folders. How do you deal with that? I think that is the underlying issue here. Last night brought them in individually but for some reason they are now not in order and some are not in the list. Thank you so much for you help!! – desertbee Apr 17 '20 at 17:29