0

我有一个在不同日期收集的空间多边形数据框列表。我想在列表中的每一天添加时间,这样我就可以对“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]
4

1 回答 1

1

pols作为您的 SpatialPolygons 列表和与“pols ”timestamp长度相同的日期向量,您可以执行以下操作:

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 
于 2015-07-02T22:52:39.803 回答