r - Points within buffer with the same id
问题描述
I have a buffer layer and a point layer:
buffer_gdf
ID
0 1A
1 1B
2 1C
and
point_gdf
ID
0 1A
1 1A
2 1A
3 1A
4 1A
5 1B
6 1B
7 1B
8 1B
9 1B
10 1B
11 1B
12 1B
13 1B
14 1C
15 1C
16 1C
17 1C
18 1C
19 1C
20 1C
21 1C
22 1C
Is there a way to count how many points with ID=1A are within buffer ID=1A, how many points ID=1B are within buffer ID=1B, how many points ID=1C are within buffer ID=1C, and so on... I have more than 20000 buffers and more than 300000 points.
I'm using pandas but I can also use R.
Sorry, I didn't mention that some points are outside the buffers. I just need those within the buffers
解决方案
Here, is one way in R
sapply(buffer_gdf$ID, function(x) sum(point_gdf$ID == x))
1A 1B 1C
5 9 9
Or with outer
rowSums(outer(buffer_gdf$ID, point_gdf$ID, `==`))
[1] 5 9 9
If this should not consider buffer_gdf
, table
would be enough
table(point_gdf$ID)
Or do a subset
and then get the table
with(point_gdf, table(ID[ID %in% buffer_gdf$ID]))
data
buffer_gdf <- structure(list(ID = c("1A", "1B", "1C")), class = "data.frame", row.names = c("0",
"1", "2"))
point_gdf <- structure(list(ID = c("1A", "1A", "1A", "1A", "1A", "1B", "1B",
"1B", "1B", "1B", "1B", "1B", "1B", "1B", "1C", "1C", "1C", "1C",
"1C", "1C", "1C", "1C", "1C")), class = "data.frame", row.names = c("0",
"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12",
"13", "14", "15", "16", "17", "18", "19", "20", "21", "22"))
推荐阅读
- excel - 基于值的重复行
- python - 如何将嵌套的 json 转换为数据框?
- vue.js - 我应该在 vue.js 中转储 data 属性中的所有变量吗
- python - 在字符串中查找单词
- android - 如何从视图模型中获取 val 中的编辑文本文本
- angular - npm -g @angular/cli (11.2) 错误
- php - 如何让 phpMyAdmin 显示旧数据
- firebase-authentication - 带有firebase auth的剃须刀页面-在哪里放置这个令牌?:)
- c - AVR ATmega328p 和 MCP3201 问题
- lisp - 输入数字的平均值