Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201867

Calculating distance between coordinates in different dataframes

$
0
0

Suppose I have the following two dataframes

dfA <- data.frame(x = rpois(10,2), y = rpois(10,2), z = rpois(10,2), q = rpois(10,2), t = rpois(10,2))
dfB <- data.frame(x = rpois(10,2), y = rpois(10,2), z = rpois(10,2), q = rpois(10,2), t = rpois(10,2))
dfAB <- map2_df(dfA, dfB, str_c, sep=",") %>%
  rename_all(~ str_c('C', seq_along(.)))

dfC <- data.frame(x = rpois(10,2), y = rpois(10,2), z = rpois(10,2), q = rpois(10,2), t = rpois(10,2))
dfD <- data.frame(x = rpois(10,2), y = rpois(10,2), z = rpois(10,2), q = rpois(10,2), t = rpois(10,2))
dfCD <- map2_df(dfC, dfD, str_c, sep=",") %>%
  rename_all(~ str_c('C', seq_along(.)))

What I looking for is to find the distance between coordinates in the first dataframe and the second, so I get a third dataframe with the distance between the first cell of dfAB and the first cell of dfCD, and the distance between the 2nd cell of dfAB and the 2nd cell of dfCD and so on; i.e. call columns C and rows R, I would like the distance between

dfAB        and     dfCD
C1 C2 C...          C1 C2 C...  
R1 R1               R1 R1   
R2 R2               R2 R2
... ...             ... ...
etc

What I am looking for is the distance between dfABC1R1 and dfCDC1R1, dfABC1R2 and dfCDC1R2, dfABC2R1 and dfCDC2R1, etc.

When I try using

dist(dfAB,dfCD)

I get the error: Error in dist(dfAB,dfCD) : invalid distance method

Any help is much appreciated


Viewing all articles
Browse latest Browse all 201867

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>