2015-07-02 22 views
0

我有一个在不同日子收集的空间多边形数据框列表。我希望为列表中的每一天添加时间,这样我就可以为“Z”(DDD.LFRP)变量设置子集并具有基于时间/日期的方法。 下面是空间的多边形列表的一个子集:将时间变量添加到R中的空间多边形数据框列表中

[[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

你想给每个'SpatialPolygonsDataF添加一个'timestamp'变量rame'在列表中? – tospig

+0

@topsig,是;用于列表中的每个图层但不包含空间多边形数据框图层中的单个多边形。即上面的两层对每一层都有一个日期/时间。 –

+0

你可以用你试过的代码更新你的问题,并且输出结果是什么? – tospig

回答

1

编写的代码与pols是您SpatialPolygons的列表和timestamp一与“pols”相同长度的日期向量,你可以这样做:

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 
+0

我想你错过了关闭')'? – tospig

+0

它与收盘工作。然而,它只是在列表的第一层。它作为一个变量而不是一个错误的日期: –

+0

我已经添加了)和示例数据以显示它的工作 – RobertH

相关问题