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

How to take value from i+1th column of 1 df and calculate distance to values in every row in i+1th column of 2nd df

$
0
0

Suppose I have the following two dataframes (with uneven rows)

set.seed(1999)
dfA <- data.frame(x = rpois(10,2), y = rpois(10,2), z = rpois(10,2), q = rpois(10,2), t = rpois(10,2))

set.seed(24)
dfB <- data.frame(a = rpois(10,2), b = rpois(10,2), c = rpois(10,2), d = rpois(10,2), e = rpois(10,2))

set.seed(10)
Dx <- sample.int(5)
set.seed(6)
Dy <- sample.int(5)

Dx <- as.data.frame(Dx)
Dx <- as.data.frame(transpose(Dx))
Dy <- as.data.frame(Dy)
Dy <- as.data.frame(transpose(Dy))

dfAB <- map2_df(dfA, dfB, str_c, sep=",") %>%
  rename_all(~ str_c('C', seq_along(.)))
dfXY <- map2_df(Dx, Dy, str_c, sep=",") %>%
  rename_all(~ str_c('C', seq_along(.)))

Now I have 2 datasets of coordinates (dfAB 5 variables each with 10 observations, dataset dfXY 5 variables with 1 observation).

What I would like to do is to find the distance between the observation of variable 1 of dfXY and every individual observation in variable 1 of dfAB, the distance between observation 1 of variable 2 of dfXY and every individual observation in variable 2 of dfAB, etc.

dfAB                          dfXY
3,1   3,2  ...       3,5  1,2  2,1  5,4  4,3   
2,1   3,1                  
2,3   1,2               
...   ...            

i.e. the distance between: a) 3,5 & 3,1 b) 3,5 & 2,1 c) 3,5 & 2,3 etc...
and the distance between: a) 1,2 & 3,2 b) 1,2 & 3,1 c) 1,2 & 1,2 etc..
and so on.

If the datasets had equal amount of observations I could use:

distances <- map2_df(
  dfAB,
  dfXY,
  ~ sqrt((.x$x - .y$x)^2 + (.x$y - .y$y)^2)
)

But since dfXY only have 1 observation (to be compared with repeatedly), this does not work. I think I need to use something like a for(i in seq_along()) function but I do not know how to incorporate the ~ sqrt((.x$x - .y$x)^2 + (.x$y - .y$y)^2)

distance <- for(i in seq_along(dfXY)){
  dfAB[,i] <- dfAB[,i] [WHAT TO PUT HERE]

Any help is much appreciated


Viewing all articles
Browse latest Browse all 211964

Trending Articles



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