英文:
How to calculate distance between all possible combinations of points within a group
问题
I have a sf object with 68 observation. In each location, I have 4 different points, with different PointIDs(SchlagID) but the same LocationIDs(FieldID). I want to now calculate the distance between each point by location in a way that there are a total of 6 values for each location (including the diagonal distances, assuming the points are in a square).
Till now, I have been able to calculate 4 values but not sure how to calculate the diagonal distances as well.
Here is what the data looks like
Here is the code I am working on:
pdist.df = data.frame(trapID=NA,LS=NA,dist=NA)
list.lsFeat = 1
counter=1
for(i in 1:nrow(raps_filtered_sf)){
this.trap=raps_filtered_sf[i,]
for(l in list.lsFeat){
this.feat = raps_filtered_sf %>%
filter(FieldID==this.trap$FieldID)
#ind <- st_nearest_feature(this.trap, this.feat)
dis <- st_distance(this.trap, this.feat)
print(paste0("trap: ", this.trap$SchlagID, "//LS: ", this.feat$SchlagID, "//Dist: ", dis))
pdist.df[counter,] = c(trapID=this.trap$SchlagID, LS=l, dist=dis)
counter=counter+1
}
}
英文:
I have a sf object with 68 observation. In each location I have 4 different points, with different PointIDs(SchlagID) but same LocationIDs(FieldID). I want to now calculate the distance between each point by location in a way that there a total of 6 values for each location (including the diagonal distances, assuming the points are in a square)
Till now I have been able to calculate 4 values but not sure how to calculate the diagonal distances as well.
Here is what the data looks like
Here is the code I am working on
pdist.df = data.frame(trapID=NA,LS=NA,dist=NA)
list.lsFeat = 1
counter=1
for(i in 1:nrow(raps_filtered_sf)){
this.trap=raps_filtered_sf[i,]
for(l in list.lsFeat){
this.feat = raps_filtered_sf %>%
filter(FieldID==this.trap$FieldID)
#ind <- st_nearest_feature(this.trap, this.feat)
dis <- st_distance(this.trap, this.feat)
print(paste0("trap: ", this.trap$SchlagID, "//LS: ", this.feat$SchlagID, "//Dist: ", dis))
pdist.df[counter,] = c(trapID=this.trap$SchlagID, LS=l, dist=dis)
counter=counter+1
}
}
答案1
得分: 1
我相信你正在寻找一个 sf::st_distance()
调用。当在单个 sf
数据框上使用它时,它会输出一个距离矩阵。
稍微调整一下名称可以更容易理解,但不是严格要求的。
举个例子,考虑下面这段代码,基于3个半随机的北卡罗来纳州城市(因为我深爱着附带 {sf} 包的 nc.shp 文件):
library(sf)
library(dplyr)
# 3 semi rancom cities in NC (because I *deeply love* the nc.shp file)
cities <- data.frame(name = c("Raleigh", "Greensboro", "Wilmington"),
x = c(-78.633333, -79.819444, -77.912222),
y = c(35.766667, 36.08, 34.223333)) %>%
st_as_sf(coords = c("x", "y"), crs = 4326)
# prepare a distance matrix
mtx <- st_distance(cities)
# give the rows & cols meaningful names
colnames(mtx) <- cities$name
rownames(mtx) <- cities$name
# see my work, and find it good...
mtx
# Units: [m]
# Raleigh Greensboro Wilmington
# Raleigh 0.0 112342.9 183751.2
# Greensboro 112342.9 0.0 269595.9
# Wilmington 183751.2 269595.9 0.0
希望这能帮助你!
英文:
I believe you are looking for a sf::st_distance()
call. When used on a single sf
data frame it will output a distance matrix.
A little finetuning of names makes it easier to interpret, but is not strictly required.
For an example consider this piece of code, built on top of 3 semi-random North Carolina cities (because I am deeply in love with the nc.shp file that ships with {sf}):
library(sf)
library(dplyr)
# 3 semi rancom cities in NC (because I *deeply love* the nc.shp file)
cities <- data.frame(name = c("Raleigh", "Greensboro", "Wilmington"),
x = c(-78.633333, -79.819444, -77.912222),
y = c(35.766667, 36.08, 34.223333)) %>%
st_as_sf(coords = c("x", "y"), crs = 4326)
# prepare a distance matrix
mtx <- st_distance(cities)
# give the rows & cols meaningful names
colnames(mtx) <- cities$name
rownames(mtx) <- cities$name
# see my work, and find it good...
mtx
# Units: [m]
# Raleigh Greensboro Wilmington
# Raleigh 0.0 112342.9 183751.2
# Greensboro 112342.9 0.0 269595.9
# Wilmington 183751.2 269595.9 0.0
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论