我有一个在不同日期收集的空间多边形数据框列表。我想在列表中的每一天添加时间,以便我可以为“Z”(DDD.LFRP)变量进行子集和基于时间/日期的方法。 下面是空间多边形列表的子集:
[[1]]
class : SpatialPolygonsDataFrame
features : 128
extent : -127.44, -67.8964, 1.000039, 31.71804 (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0
variables : 1
names : DDD.LFRP
min values : 16.6
max values : 488.5
[[2]]
class : SpatialPolygonsDataFrame
features : 126
extent : -129.04, -67.8964, 3.759985, 31.71804 (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0
variables : 1
names : DDD.LFRP
min values : 14
max values : 335.2
如何添加添加时间/日期呢? 时间戳是从文件名中的一组字符串子集创建的:
##Define date string
regexp2<-"_([[:digit:]]{12})"
DatesDL3<-sapply(names(DL3), function(x)stri_extract_first_regex(x,regexp2))
DatesL<-gsub("_", "", DatesDL3)
DDO2<-data.frame(DatesL)
DDO3<-DDO2[["DatesL"]]
#COnvert straight to dates
Timex<-strptime(DDO3, "%Y%m%d%H%M")
Timex
[1] "2008-12-01 04:00:00 GMT" "2008-12-01 06:30:00 GMT"
然后我尝试了@RobertH写的代码
polsdatetime <- lapply(1:length(DL3), function(i) {
p <- DL3[[i]]
p$datetime <- Timex[i]
p
})
polsdatetime
[[1]]
class : SpatialPolygonsDataFrame
features : 119
extent : -124.23, -68.26758, 2.141337, 31.80002 (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0
variables : 2
Error in as.matrix.data.frame(X) :
dims [product 119] do not match the length of object [130]
答案 0 :(得分:1)
pols
是您的SpatialPolygons列表,timestamp
是一个与`pols&#39;相同长度的日期向量你可以这样做:
library(raster)
p <- shapefile(system.file("external/lux.shp", package="raster"))
pols <- list(p,p)
timestamp <- c("2008-12-05 21:30:00 GMT", "2008-12-05 22:45:00 GMT")
polsdatetime <- lapply(1:length(pols), function(i) {
p <- pols[[i]]
p$datetime <- timestamp[i]
p
} )
polsdatetime
# [[1]]
# class : SpatialPolygonsDataFrame
# features : 12
# extent : 5.74414, 6.528252, 49.44781, 50.18162 (xmin, xmax, ymin, ymax)
# coord. ref. : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0
# variables : 6
# names : ID_1, NAME_1, ID_2, NAME_2, AREA, datetime
# min values : 1, Diekirch, 1, Capellen, 76, 2008-12-05 21:30:00 GMT
# max values : 3, Luxembourg, 12, Wiltz, 312, 2008-12-05 21:30:00 GMT
# [[2]]
# class : SpatialPolygonsDataFrame
# features : 12
# extent : 5.74414, 6.528252, 49.44781, 50.18162 (xmin, xmax, ymin, ymax)
# coord. ref. : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0
# variables : 6
# names : ID_1, NAME_1, ID_2, NAME_2, AREA, datetime
# min values : 1, Diekirch, 1, Capellen, 76, 2008-12-05 22:45:00 GMT
# max values : 3, Luxembourg, 12, Wiltz, 312, 2008-12-05 22:45:00 GMT