Good morning,

I would like to stack a raster dataset taking into account the number of days between images. Sometimes the number of dates between observations are 8, 16 or 365 days. Is there any function to do it automatically?

The code that I have right now is as follows:

rlist=list.files(pattern=".tif$", full.names=T)

I guess that this code does not count the separation between days and has the same interval for all the images...?

The name of the files is: NDVI.2000_01_01_NDVI is it correct or should I give a special name to be able to determine the date in each image?

  • Can use setZ(rasters, dates) to give it arbitrary dates, you could parse that out with as.Date("NDVI.2000_01_01_NDVI", "NDVI.%Y_%m_%d"). I don't think any in-built functions take account of the z-spacing, though. You can access it with getZ() – mdsumner Oct 17 '18 at 10:09
  • Thank you but, I am a beginner so I do not really know how to introduce it in my code. Could you let me know how, please? – J Richardson Oct 17 '18 at 10:20

Create a stack:

> mystack = stack(lapply(1:5, function(...){raster(matrix(runif(24),3,8))}))

Set Z to some arbitrary values:

> mystack = setZ(mystack,c(1,23,44,55,901))

Get them back:

> getZ(mystack)
[1]   1  23  44  55 901

If you want the Zs to be dates, make them dates:

> mystack = setZ(mystack,as.Date(paste0("2001-01-0",1:5)))
> getZ(mystack)
[1] "2001-01-01" "2001-01-02" "2001-01-03" "2001-01-04" "2001-01-05"

Pulling the dates out of your filenames (if that's what you need to do) is not GIS-related so if you struggle with that ask on www.stackoverflow.com

Most functions that work on stacks will not take into account your Z values though - if you need to do some analysis that depends on layer 1 being 23 days after layer 2 and layer 2 being 76 days after layer 3 then you have to pull out the Z values yourself to work with them.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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