我试图计算伦敦每个LSOA区域内的积分数。我试图使用 over 函数,尽管输出不会产生每个LSOA列表数量的计数
我到目前为止所执行的代码如下
ldnLSOA <- readOGR(".", "LSOA_2011_London_gen_MHW")
LondonListings <- read.csv('Londonlistings.csv')
proj4string(LdnLSOA) <- proj4string(LondonListings)
plot(ldnLSOA)
plot(LondonListings, add =T)
LSOAcounts <- over(LondonListings, ldnLSOA)
这会生成一个没有原始 ldnLSOA shapefile的附加数据的表。
我想知道是否有人知道如何以格式获得表格:
LSOAname | LSOAcode | Count
或那种框架。
示例数据:
LondonListings:
longitude | latituide
-0.204406 51.52060
-0.034617 51.45037
-0.221920 51.46449
-0.126562 51.47158
-0.188879 51.57068
-0.096917 51.49281
Shapefile:
https://data.london.gov.uk/dataset/statistical-gis-boundary-files-london
答案 0 :(得分:1)
我删除了我的特定答案,并用你的数据写了另一个(除了点......但是替换这些数据并不难,对吧?) 让我知道它是否有效!
#I'm not sure which of this libs are used, since I always have all of them loaded here
library(rgeos)
library(rgdal)
library(sp)
#Load the shapefile
ldnLSOA <- readOGR(".", "LSOA_2011_London_gen_MHW")
plot(ldnLSOA)
#It's always good to take a look in the data associated to your map
ldn_data<-as.data.frame(ldnLSOA@data)
#Create some random point in this shapefile
ldn_points<-spsample(ldnLSOA,n=1000, type="random")
plot(ldnLSOA)
plot(ldn_points, pch=21, cex=0.5, col="red", add=TRUE)
#create an empty df with as many rows as polygons in the shapefile
df<-as.data.frame(matrix(ncol=3, nrow=length(ldnLSOA@data$LSOA11NM)))
colnames(df)<- c("LSOA_name","LSOA_code", "pt_Count")
df$LSOAname<-ldn_data$LSOA11NM
df$LSOAcode<-ldn_data$LSOA11CD
# Over = at the spatial locations of object x,
# retrieves the indexes or attributes from spatial object y
pt.poly <- over(ldn_points,ldnLSOA)
# Now let's count
pt.count<-as.data.frame(table(pt.poly$LSOA11CD))
#As it came in alphabetical order, let's put in the same order of data in data frame
pt.count_ord<-as.data.frame(pt.count[match(df$LSOA_name,pt.count$Var1),])
#Fill 3rd col with counts
df[,3]<-pt.count_ord$Freq