我有一个多年同一位置的栅格列表。像素值随时间的变化表示像素的时间序列。 为了进一步分析,我需要提取每个像素的时间值,并将其存储在数据框中,其中row = #pixel,column = year
虚拟数据:
library(raster)
# create raster data from scratch
# create empty raster
y1<-raster(ncol = 3, nrow = 3)
values(y1)<-1:9
projection(y1)<-CRS("+init=epsg:4326")
# create and diversify the rasters
y2<-y1+10
y3<-y1+20
y4<-y1+30
# make list of rasters
y.list<-list(y1, y2,y3,y4)
# plot all rasters at once
par(mfrow = c(2,2))
for(i in 1:length(y.list)) {
plot(y.list[[i]])
}
数据框的外观如下:
y1 y2 y3 y4
pixel1 1 10 20 30
pixel2
...
pixel9 9 19 29 39
如何随时间提取唯一像素值,并将单个像素数据转换为数据帧?
答案 0 :(得分:1)
我在这里找到了一个很好的答案! How to extract values from rasterstack with xy coordinates?
无需将栅格放入栅格列表中 - 只需创建栅格堆栈!!
然后,只需使用raster :: extract创建每个像素值的时间序列!
整个剧本:
library(raster)
# create raster data from scratch
# create empty raster
y1<-raster(ncol = 3, nrow = 3)
values(y1)<-1:9
projection(y1)<-CRS("+init=epsg:4326")
# recreate and diversify the rasters
y2<-y1+10
y3<-y1+20
y4<-y1+30
# create raster stack
# create raster stack
s<-stack(y1, y2, y3, y4)
# plot rasters
plot(s)
# extract raster values - return a matrix of values in each pixel
# row = pixel, column = layer (year)
mat <- raster::extract( s , 1:ncell(s) )
tadaaaa !!!!
> mat
layer.1 layer.2 layer.3 layer.4
[1,] 1 11 21 31
[2,] 2 12 22 32
[3,] 3 13 23 33
[4,] 4 14 24 34
[5,] 5 15 25 35
[6,] 6 16 26 36
[7,] 7 17 27 37
[8,] 8 18 28 38
[9,] 9 19 29 39