I have been trying to sum my rasterlayers from my rasterstack for each month by their respective years. Similarly to this thread here: Sum nlayers of a rasterStack in R
I have been able to change the names of my raster layers to a date format yy.mm.dd
using this:
p <- names(landcover) %>%
str_extract("(?<=MCD64A1.A)[0-9]{4}") %>%
paste(outer(1:12,1:10, paste, sep="-" ), sep="-")
setNames(landcover, 1)
landcover
[1] "2010-1-1" "2010-2-1" "2010-3-1" "2010-4-1"
[5] "2010-5-1" "2010-6-1" "2010-7-1" "2010-8-1"
[9] "2010-9-1" "2010-10-1" "2010-11-1" "2010-12-1"
[13] "2011-1-2" "2011-2-2" "2011-3-2" "2011-4-2"
[17] "2011-5-2" "2011-6-2" "2011-7-2" "2011-8-2"
[21] "2011-9-2" "2011-10-2" "2011-11-2" "2011-12-2"
[25] "2012-1-3" "2012-2-3" "2012-3-3" "2012-4-3"
[29] "2012-5-3" "2012-6-3" "2012-7-3" "2012-8-3"
[33] "2012-9-3" "2012-10-3" "2012-11-3" "2012-12-3"
[37] "2013-1-4" "2013-2-4" "2013-3-4" "2013-4-4"
[41] "2013-5-4" "2013-6-4" "2013-7-4" "2013-8-4"
[45] "2013-9-4" "2013-10-4" "2013-11-4" "2013-12-4"
[49] "2014-1-5" "2014-2-5" "2014-3-5" "2014-4-5"
[53] "2014-5-5" "2014-6-5" "2014-7-5" "2014-8-5"
[57] "2014-9-5" "2014-10-5" "2014-11-5" "2014-12-5"
[61] "2015-1-6" "2015-2-6" "2015-3-6" "2015-4-6"
[65] "2015-5-6" "2015-6-6" "2015-7-6" "2015-8-6"
[69] "2015-9-6" "2015-10-6" "2015-11-6" "2015-12-6"
[73] "2016-1-7" "2016-2-7" "2016-3-7" "2016-4-7"
[77] "2016-5-7" "2016-6-7" "2016-7-7" "2016-8-7"
[81] "2016-9-7" "2016-10-7" "2016-11-7" "2016-12-7"
[85] "2017-1-8" "2017-2-8" "2017-3-8" "2017-4-8"
[89] "2017-5-8" "2017-6-8" "2017-7-8" "2017-8-8"
[93] "2017-9-8" "2017-10-8" "2017-11-8" "2017-12-8"
[97] "2018-1-9" "2018-2-9" "2018-3-9" "2018-4-9"
[101] "2018-5-9" "2018-6-9" "2018-7-9" "2018-8-9"
[105] "2018-9-9" "2018-10-9" "2018-11-9" "2018-12-9"
[109] "2019-1-10" "2019-2-10" "2019-3-10" "2019-4-10"
[113] "2019-5-10" "2019-6-10" "2019-7-10" "2019-8-10"
[117] "2019-9-10" "2019-10-10" "2019-11-10" "2019-12-10"
Next by following the thread above, I used this code:
indices <- format(as.Date(names(p), format = "%Y.%m.%d"), format = "%Y")
print(indices)
character(0)
However, it returns character(0)
, as opposed to formatting by each year.
my aim is to use this code:
datasum<- stackApply(data, indices, fun = sum)
So I am only left with years 2010 to 2019, as thats the extent of which i am working with.