Preserve/store 通过 R 中的栅格循环
Preserve/store through raster loop in R
我有一个很大的栅格堆栈,我计算每两层的最大值。除了
日期。如何将日期保存到我的新堆栈中? (我假设我需要 getZ
但我不知道如何实现它。)
谢谢
示例代码
m_date<-as.data.frame(as.Date(c('2009-05-01','2010-03-25','2007-06-14','2008-12-18','2016-05-20')))
m_r <- raster(matrix(1:60,3,4))
m_stack <- stack(m_r,m_r,m_r,m_r,m_r)
m_stack <- addLayer(m_stack,m_date)
new_stack<-m_stack[[1]]
new_stackk@z$Date<-m_stack@z$Date[[1]]
for (i in 1:5) {
# calculate max every two layers (works fine)
new_stack[[k]] <- calc(m_stack[[((i-1)*2 + 1):((i-1)*2 + 2)]], fun = max, na.rm = T)
# store the date for each one of the max layers (does not work)
new_stack@z$Date[[i]] <-m_stack@z$Date[[i]]
}
这是一些运行的代码。它假设:
- 您希望比较相邻的日期(图层按日期排序)
library(raster)
m_date <- data.frame(date =
as.Date(c('2009-05-01','2010-03-25','2007-06-14','2008-12-18')))
m_r <- lapply(m_date$date, function(x) raster(matrix(rnorm(60),3,4)))
m_stack <- stack(m_r)
m_stack@z <- list(m_date = m_date[,1])
# sort by date
m_stack <- m_stack[[order(m_stack@z$m_date)]]
new_stack <- m_stack[[-1]]
for (i in 1:(nlayers(new_stack))) {
new_stack[[i]] <- calc(m_stack[[i:(i+1)]], fun = max)
new_stack@z$m_date[[i]] <- m_stack@z$m_date[i:(i+1)][
which.max(c(
cellStats(m_stack[[i]], stat = max),
cellStats(m_stack[[i+1]], stat = max)))]
}
new stack
dimensions : 3, 4, 12, 3 (nrow, ncol, ncell, nlayers)
resolution : 0.25, 0.3333333 (x, y)
extent : 0, 1, 0, 1 (xmin, xmax, ymin, ymax)
coord. ref. : NA
names : layer.1, layer.2, layer.3
min values : -0.03990727, -0.46295595, -0.39121706
max values : 2.261900, 2.261900, 1.647811
m_date : 2008-12-18, 2008-12-18, 2009-05-01
我有一个很大的栅格堆栈,我计算每两层的最大值。除了
日期。如何将日期保存到我的新堆栈中? (我假设我需要 getZ
但我不知道如何实现它。)
谢谢
示例代码
m_date<-as.data.frame(as.Date(c('2009-05-01','2010-03-25','2007-06-14','2008-12-18','2016-05-20')))
m_r <- raster(matrix(1:60,3,4))
m_stack <- stack(m_r,m_r,m_r,m_r,m_r)
m_stack <- addLayer(m_stack,m_date)
new_stack<-m_stack[[1]]
new_stackk@z$Date<-m_stack@z$Date[[1]]
for (i in 1:5) {
# calculate max every two layers (works fine)
new_stack[[k]] <- calc(m_stack[[((i-1)*2 + 1):((i-1)*2 + 2)]], fun = max, na.rm = T)
# store the date for each one of the max layers (does not work)
new_stack@z$Date[[i]] <-m_stack@z$Date[[i]]
}
这是一些运行的代码。它假设:
- 您希望比较相邻的日期(图层按日期排序)
library(raster)
m_date <- data.frame(date =
as.Date(c('2009-05-01','2010-03-25','2007-06-14','2008-12-18')))
m_r <- lapply(m_date$date, function(x) raster(matrix(rnorm(60),3,4)))
m_stack <- stack(m_r)
m_stack@z <- list(m_date = m_date[,1])
# sort by date
m_stack <- m_stack[[order(m_stack@z$m_date)]]
new_stack <- m_stack[[-1]]
for (i in 1:(nlayers(new_stack))) {
new_stack[[i]] <- calc(m_stack[[i:(i+1)]], fun = max)
new_stack@z$m_date[[i]] <- m_stack@z$m_date[i:(i+1)][
which.max(c(
cellStats(m_stack[[i]], stat = max),
cellStats(m_stack[[i+1]], stat = max)))]
}
new stack
dimensions : 3, 4, 12, 3 (nrow, ncol, ncell, nlayers) resolution : 0.25, 0.3333333 (x, y) extent : 0, 1, 0, 1 (xmin, xmax, ymin, ymax) coord. ref. : NA names : layer.1, layer.2, layer.3 min values : -0.03990727, -0.46295595, -0.39121706 max values : 2.261900, 2.261900, 1.647811 m_date : 2008-12-18, 2008-12-18, 2009-05-01