【问题标题】:R - combine character and numeric columns in SpatialPolygonsDataFrameR - 在 SpatialPolygonsDataFrame 中组合字符和数字列
【发布时间】:2019-04-20 04:28:05
【问题描述】:

我想找到一种有效的方法来组合 SpatialPolygonsDataFrame 对象列表中的某些字符 + 数字列值。这是可重现的数据:

library(maptools)  ## For wrld_simpl
library(sp)

## Example SpatialPolygonsDataFrames (SPDF)
data(wrld_simpl) #polygon of world countries
spdf1 <- wrld_simpl[1:25,] #country subset 1
spdf2 <- wrld_simpl[26:36,] #subset 2
spdf3 <- wrld_simpl[36:50,] #subset 3

#make list of SPDF objects
spdfl<-list()
spdfl[[1]]<-spdf1
spdfl[[2]]<-spdf2
spdfl[[3]]<-spdf3

#view data (attribute table) for one list element
spdfl[[1]]@data

我想做的是添加另一列,它是 FIPS、REGION 和 SUBREGION 列的组合,用下划线 ('_') 分隔。我知道如何在下面的循环中为列表中的每个 SPDF 对象添加+命名一个新列,但我不知道如何获取所需的列行条目:

#add new 'unique.id' column to SPDF
for (i in 1:length(spdfl)){
  spdfl[[i]]@data["unique.id"] = ""
}

新的 unique.id 列的行条目将采用以下格式:FIPS_REGION_SUBREGION。例如,对于 spdfl[[1]] 中的 ATG 多边形特征,我希望 'unique.id' 列有这样的条目:

unique.id
AC_19_29

请告知如何对 SPDF 列表中的所有功能执行此操作。

【问题讨论】:

    标签: r spatial-data-frame


    【解决方案1】:
    spdfl[[1]]@data$unique.id<- 
    paste(spdfl[[1]]@data$FIPS,spdfl[[1]]@data$REGION,spdfl[[1]]@data$SUBREGION,sep="_")
    

    编辑:对于您想要的循环行为:

    for (i in 1:length(spdfl)){
      spdfl[[i]]@data$unique.id<- 
      paste(spdfl[[i]]@data$FIPS,spdfl[[i]]@data$REGION,
      spdfl[[i]]@data$SUBREGION,sep="_")
      }
    

    【讨论】:

    • 非常感谢@Dan Woodrich!正是我需要的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-31
    • 1970-01-01
    • 2017-03-13
    相关资源
    最近更新 更多