簡體   English   中英

使用gdist()計算分組子集的點之間的距離

[英]Calculate distance between points using gdist() for grouped subsets

我的2個人數據的子集( squirrelID )可以在這里找到。

我的數據如下所示(僅顯示相關列):

lat                  lon                NatalMidden   squirrelID    type
60.9577819984406    -138.0347849708050  -27           NA            Nest2017
60.9574120212346    -138.0345689691600  -27           NA            NatalMidden
60.9578209742904    -138.0346520338210  -27           23054         Foray
60.9575380012393    -138.0348329991100  -27           23054         Foray
60.9576250053942    -138.0339069664480  -27           23054         Foray
60.957643026486     -138.0338829942050  -27           23054         Foray
60.9575670026243    -138.0348739866170  -27           23054         Foray

例如,對於squirrelID 23054,它被多次定位( Foray )( type列),並且每個Foray都有對應的緯度( lat )和經度( lon )。 我正在嘗試分別為每個人( squirrelID )計算每個Foraytype列)和Nest2017type列)之間的距離。

下面的代碼有效(並給了我15.11501 m的值),但是它要求我手動輸入每個點。 可以說,這沒有問題,但是我正在處理+2000個觀察值,每個gridNatalMiddensquirrelID列有2個以上的選項。

library(Imap)

gdist(60.9578209742904,-138.0346520338210, 60.9577819984406, -138.0347849708050, units="m", verbose=FALSE)

有沒有一種方法,我可以在中工作 dplyr 框架 group_by(squirrelID) 然后計算每個之間的距離 Foray 及其相應 Nest2017 (具有相同 NatalMidden 同時為 Foray Nest2017 )?

我的最終目標是為每個squirrelID創建一個ForayNest2017之間的距離的新列。

更新:

我嘗試了以下方法:

nests<-df %>% #creating a new data frame for Nest2017 points only
    filter(type %in% "Nest2017") %>%
    select(ID,lat,lon,ele,grid,NatalMidden,type)

foray<-df %>% #creating a new data frame for Foray points only
    filter(type %in% "Foray") %>%
    mutate(sq_id=as.factor(sq_id)) %>%
    group_by(sq_id)

但是這些子集在gdist函數中不起作用(我收到此錯誤):

gdist(nests$lat, nests$lon, foray$lat, foray$lon, units="m", verbose=FALSE)

Error in while (abs(lamda - lamda.old) > 1e-11) { : 
  missing value where TRUE/FALSE needed
In addition: Warning messages:
1: In Ops.factor(lon.1, rad) : ‘*’ not meaningful for factors
2: In Ops.factor(lat.1, rad) : ‘*’ not meaningful for factors
3: In Ops.factor(lon.2, rad) : ‘*’ not meaningful for factors
4: In Ops.factor(lat.2, rad) : ‘*’ not meaningful for factors
5: In lon.1 - lon.2 :
  longer object length is not a multiple of shorter object length
6: In while (abs(lamda - lamda.old) > 1e-11) { :
  the condition has length > 1 and only the first element will be used

我對dplyr軟件包不是很熟悉,但是我認為這會做您感興趣的事情:

# read data from the FigShare linked file
squirrel_data <- read.table("figshare.txt", header=T)

# split into 'Forays' and 'Nests'
nests <- squirrel_data %>%
  filter(type %in% "Nest2017")
foray <- squirrel_data %>%
  filter(type %in% "Foray")

# merge 'Forays' and 'Nests' by 'NatalMidden'
nests_foray <- inner_join(
  nests, foray, by = "NatalMidden", suffix = c(".nest", ".foray"))

# calculate the distance for each row, keep 'SquirrelID' and 'Dist'
results <- nests_foray %>%
  rowwise() %>%
  mutate(dist = gdist(lat.nest, lon.nest,
                      lat.foray, lon.foray, units = "m")) %>%
  select(squirrelID.foray, dist)
head(results, n = 3)
## A tibble: 3 x 2
#  squirrelID.foray     dist
#             <int>    <dbl>
#1            22684 14.03843
#2            22684 59.06996
#3            22684 13.40567

這基本上是我在第dplyr提出的內容,但是使用dplyr函數而不是base 這個想法只是通過“ NatalMidded”在“ Foray”行和“ Nest2017”行之間創建內部聯接,然后簡單地計算每行的距離並使用“ SquirrelID”報告它。 我希望這有幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM