如何解决越过两组坐标以计算最小距离? tidyr地球圈R
在同一边界区域中,我有一组2,220个嵌套坐标(var1)和另一组26个界标坐标(var2)。我想找到2624组中每个点之间的2,224个坐标之间的距离,以便创建带有列(嵌套坐标,最小距离界标坐标,以m为单位的距离)的新数据框。
我被困在尝试交叉这两个集合以生成一个集合,其中所有界标坐标与每个嵌套坐标配对。
**nest** **landmark** **distance**
lat1,lon1 lat1,lon1 34
lat1,lon1 lat2,lon2 18
lat1,lon1 lat3,lon3 82
....
lat1,lon1 lat26,lon26 61
lat2,lon2 lat1,lon1 94
lat2,lon2 lat2,lon2 38
...
lat2,220,lon 2,220 lat 26,lon26 46
我尝试过交(var1,var2),其中var1和var2都是包含经度和纬度值的矩阵,然后计算每个结果行之间的Haversine距离(请参见下文)。这似乎可行,但是我认为这并没有给我期望的确切结果。交叉产生的行数与这些集合的nrow的乘积不一致。
我还希望能够将具有所有距离值的结果集分成26个组,其中每个组包含嵌套坐标(每行重复),26个界标坐标之一以及两点。从那里,我将选择距离最小的行。
newset <- crossing(nests,landmarks)
mindist <- distHaversine(newset[1],newset[2],r=6378137)
newsetwdist <- cbind(newset,mindist)
sv <- split(newsetwdist,rep(1:56056,each=26))
#56056 was the resulting number of rows,even though I expected 57,720.
var3 <- lapply(sv,"[",3) #returns a nested list of all distances for each nest
var4 <- lapply(var2,"[[","mindist")
df = as.data.frame(do.call(rbind,lapply(var4,unlist)))
min.dist.from.landmark <- apply(df,1,FUN=min)
似乎应该是一个简单的解决方法,我们将不胜感激。
解决方法
使用我为此创建的数据和数据格式,您可以执行以下操作。
N = 10 # Number of generations (years)
S = 50 # Number of seeds each pea plant produces
P0 = 20 # Number of pea plants in year 0
w = 0.25 # Fraction of seeds that survive the winter (survival rate)
g = [0.0,0.02,0.04,0.06,0.08,0.10,0.12,0.14,0.16] # Fraction of seeds that germinate (germination rate)
P = [] # Number of pea plants
P.append(P0) # Append initial number of pea plants
t = range(N) # Years
remaining_seeds = [] # One-year-old seeds that did not germinate and remained in the ground
remaining_seeds.append(0) # In year 0 this is 0
n = 1 # Generation (year)
while n < N:
produced_seeds = S * P[n-1]
surviving_seeds = produced_seeds*w
plants_1_year = choice(g) * surviving_seeds
remaining_seeds_n = (1- choice(g))*surviving_seeds
remaining_seeds.append(remaining_seeds_n)
surviving_seeds_2 = w * remaining_seeds[n-1]
plants_2_year = choice(g)*surviving_seeds_2
P_n = plants_1_year+plants_2_year
P.append(P_n)
n = n + 1
plot(t,P,"-o")
xlabel("Generation,t")
ylabel("Number of pea plants,P")
title("Plant population when seeds survive two winters,with variable germination rate")
show()
数据
library(dplyr)
library(purrr)
library(tidyr)
library(geosphere)
crossing(nest,landmark) %>%
mutate(nest_long_lat = map2(nest_long,nest_lat,~ c(.x,.y)),mark_long_lat = map2(mark_long,mark_lat,distance = unlist(map2(mark_long_lat,nest_long_lat,~ distGeo(.x,.y)))) %>%
group_by(nest_long_lat) %>%
mutate(min_distance = distance == min(distance)) %>%
ungroup() %>%
select(-nest_long_lat,-mark_long_lat)
# # A tibble: 57,720 x 6
# nest_lat nest_long mark_lat mark_long distance min_distance
# <dbl> <dbl> <dbl> <dbl> <dbl> <lgl>
# 1 46.5 49.1 48.4 49.8 215350. TRUE
# 2 46.5 49.1 48.6 48.7 229592. FALSE
# 3 46.5 49.1 48.8 49.9 255689. FALSE
# 4 46.5 49.1 48.9 48.4 268789. FALSE
# 5 46.5 49.1 49.3 50.1 312691. FALSE
# 6 46.5 49.1 49.3 49.2 309549. FALSE
# 7 46.5 49.1 49.6 51.6 390862. FALSE
# 8 46.5 49.1 49.7 50.8 371686. FALSE
# 9 46.5 49.1 49.8 50.6 377182. FALSE
# 10 46.5 49.1 49.9 49.9 376530. FALSE
# # … with 57,710 more rows
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。