简体   繁体   中英

Calculating the distance between coordinates R

We have a set of 50 csv files from participants, currently being read into a list as

file_paths <- fs::dir_ls("data")
file_paths

file_contents <- list ()

for (i in seq_along (file_paths)) {
  file_contents[[i]] <- read_csv(
    file = file_paths[[i]]
  )
}

dt <- set_names(file_contents, file_paths)

My data looks like this:

level time     X        Y       Type
    
 1     1  355. -10.6    22.36    P
 1     1  371. -33      24.85    O
 1     2  389. -10.58   17.23    P
 1     2  402. -16.7    30.46    O 
 1     3  419. -29.41   17.32    P 
 1     4  429. -10.28   26.36    O 
 2     5  438. -26.86   32.98    P
 2     6  451. -21      17.06    O 
 2     7  463. -21      32.98    P 
 2     8  474. -19.9    17.06    O 

We have 70 sets of coordinates per csv. Time does not matter for this, but I would like to split up by the level column at some stage.

For every 'P' I want to compare it to 'O' and get the distance between coordinates.The first P will always match with the first O and so on.

For now, I have them split into two different lists, though this may be the complete wrong way to do it, I'm having trouble figuring out how to take all of these csv files and get the distances for all of them, the list seems to cause issues with most functions (like dist)

Here is how I've pulled the right information so far


for (i in seq_along (dt)) {

   pLoc[[i]] <- dplyr::filter(dt[[i]], grepl("P", type)) 
   oLoc[[i]] <- dplyr::filter(dt[[i]], grepl("o", type)) 


   pX[[i]] <- pLoc[[i]] %>% pull(as.numeric(headX)) 
   pY[[i]] <- pLoc[[i]] %>% pull(as.numeric(headY)) 
    
   pCoordinates[[i]] <- cbind(pX[[i]], pY[[i]])
}

[EDITED] Following comments, here is how you can do it with the raster library:

library(raster)
library(dplyr)

df = data.frame(
  x = c(10, 20 ,15,9),
  y = c(45,34,54,24),
  type = c("P","O","P","O")
)

df = cbind(df[df$type=="P",]  %>% 
             dplyr::select(-type) %>% 
             dplyr::rename(xP = x,
                    yP = y),
           df[df$type=="O",] %>% 
             dplyr::select(-type) %>% 
             dplyr::rename(xO = x,
                    yO = y))

The following could probably be achieved more efficiently with some form of the apply() function:

v = c()

for(i in 1:nrow(df)){
  
  dist = raster::pointDistance(lonlat = F,
                               p1 = c(df$xP[i],df$yP[i]),
                               p2 = c(df$xO[i],df$yO[i]))
  
  v = c(v,dist)
  
}

df$dist = v

print(df)
xP yP xO yO     dist
1 10 45 20 34 14.86607
3 15 54  9 24 30.59412

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM