【发布时间】:2019-04-11 20:17:25
【问题描述】:
我有许多SpatialLinesDataFrames 的列表。我想向每个 SLDF 添加一列,其 ID 与列表索引 ID 等效(即,每个单独的 SLDF 新列中的每一行都将具有相同的 ID)。我希望该解决方案适用于任何类型的 sp Spatial DataFrame 对象(多边形、点等)。
基于简单 data.frames (Assign unique ID to each data.frame element in a list) 的解决方案,我使用以下示例代码进行了尝试:
library(raster)
#create list of single-feature `SpatialLineDataFrame`
l1 <- cbind(c(0,3), c(0,3))
l2 <- cbind(c(0, 13), c(0, 1))
l3 <- cbind(c(0, 24), c(0,22.5))
l4 <- cbind(c(0, 1), c(0,13))
l5 <- cbind(c(0, 6), c(0,6))
Sldf <- spLines(l1, l2, l3, l4, l5, attr=data.frame(lineID=1:5))
#make individual list elements
sldfl <- list()
sldfl[[1]] <- Sldf[1,]
sldfl[[2]] <- Sldf[2,]
sldfl[[3]] <- Sldf[3,]
sldfl[[4]] <- Sldf[4,]
sldfl[[5]] <- Sldf[5,]
#attempt to add new column with unique index id
newlist <- Map(cbind,sldfl, unique.id = (1:length(sldfl)))
我希望列名称为“unique.id”并且所有元素都相同,但结果是特定于元素的,而不是我指定的名称(即 X1L、X2L 等),如下所示:
[[1]]
class : SpatialLinesDataFrame
features : 1
extent : 0, 3, 0, 3 (xmin, xmax, ymin, ymax)
coord. ref. : NA
variables : 2
names : lineID, X1L
value : 1, 1
[[2]]
class : SpatialLinesDataFrame
features : 1
extent : 0, 13, 0, 1 (xmin, xmax, ymin, ymax)
coord. ref. : NA
variables : 2
names : lineID, X2L
value : 2, 2
但我想要这个:
[[1]]
class : SpatialLinesDataFrame
features : 1
extent : 0, 3, 0, 3 (xmin, xmax, ymin, ymax)
coord. ref. : NA
variables : 2
names : lineID, unique.id
value : 1, 1
[[2]]
class : SpatialLinesDataFrame
features : 1
extent : 0, 13, 0, 1 (xmin, xmax, ymin, ymax)
coord. ref. : NA
variables : 2
names : lineID, unique.id
value : 2, 2
【问题讨论】:
-
spLines来自哪个包?似乎不在sp... -
另外,请说明如何在 one sp 数据框上使用
unique.id重命名?然后,我们可以帮助您遍历列表中的所有内容。 -
对不起@Gregor - raster::spLines。将相应地编辑代码
-
@Parfait 除了调用单个特定列表元素之外,我不知道如何使用一个 sp 数据帧重命名,例如 newlist1
标签: r list loops dataframe geospatial