我想找到一种有效的方法来组合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列表中的所有功能提供建议。
答案 0 :(得分:5)
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="_")
}