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 is a new dataframe with 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.
If we have
dfAB dfXY
3,1 3,2 ... 3,5 1,2 2,1 5,4 4,3
2,1 3,1
2,3 1,2
... ...
Then the new dataframe should contain 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.
dfABXY
3,1&3,5 3,2&1,2
2,1&3,5 3,1&1,2
2,3&3,5 1,2&1,2
However when I use:
distances <- map2_df(
dfAB,
dfXY,
~ sqrt((.x$x - .y$x)^2 + (.x$y - .y$y)^2)
)
I get the error Error in .x$x : $ operator is invalid for atomic vectors
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]
Please note that my actual dataset is thousands of rows and columns so I calling rows/columns manually is not viable.
Any help is much appreciated