【问题标题】:R - assign unique ID to each Spatial_DataFrame element in a listR - 为列表中的每个 Spatial_DataFrame 元素分配唯一 ID
【发布时间】: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


【解决方案1】:

这是一个简单的 for 循环。

for (i in seq_along(sldfl)) {
  sldfl[[i]]@data = cbind(sldfl[[i]]@data, unique.id = i)
}

sldfl
# [[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 
# ...

您尝试的Map 方法的问题是cbind 不适用于将列添加到SpatialLinesDataFrame,您需要在@data 插槽上显式使用cbind。我们可以使用Map,但我发现显式 for 循环更清晰,特别是如果您想就地修改对象。

newlist <- Map(function(x, y) cbind(x@data, unique.id = y), sldfl,  1:length(sldfl))

【讨论】:

    【解决方案2】:

    这里有一个更好的方法来做 Gregor 建议的事情

    for (i in seq_along(sldfl)) {
      sldfl[[i]]$unique.id <- i
    }
    

    【讨论】:

      猜你喜欢
      • 2019-09-02
      • 1970-01-01
      • 2019-12-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-07-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多