Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 每个LSOA(多边形)内的计数点_R_Count_Polygon_Shapefile_Point In Polygon - Fatal编程技术网

R 每个LSOA(多边形)内的计数点

R 每个LSOA(多边形)内的计数点,r,count,polygon,shapefile,point-in-polygon,R,Count,Polygon,Shapefile,Point In Polygon,我试图计算伦敦各LSOA区域内的点数。我尝试使用over函数,尽管输出没有生成每个LSOA的列表数 到目前为止,我执行的代码如下 ldnLSOA <- readOGR(".", "LSOA_2011_London_gen_MHW") LondonListings <- read.csv('Londonlistings.csv') proj4string(LdnLSOA) <- proj4string(LondonListings) plot(ldnLSOA) plot(L

我试图计算伦敦各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) 
或者类似的框架

示例数据:

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
形状文件:


我删除了我的具体答案,并用你的数据写了另一个答案,除了分数。。。但替换这些数据并不难,对吗? 让我知道它是否有效

#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

请提供一个可重复使用的示例。我不确定如何添加信息以使shapefile可复制,但我已添加了伦敦列表的示例数据您需要使用内置或公开可用的数据文件shapefile可在以下位置找到:
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
#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