假设我有以下两个数据框
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(.)))
我要寻找的是找到第一个数据帧和第二个数据帧中坐标之间的距离,因此我得到了第三个数据帧,其中包含dfAB的第一个单元格与dfCD的第一个单元格之间的距离,以及dfAB的第二个单元格之间的距离dfCD的第二个单元,依此类推;即调用列C和行R,我想之间的距离
dfAB and dfCD
C1 C2 C... C1 C2 C...
R1 R1 R1 R1
R2 R2 R2 R2
... ... ... ...
etc
我正在寻找的是dfABC1R1和dfCDC1R1,dfABC1R2和dfCDC1R2,dfABC2R1和dfCDC2R1等之间的距离。
当我尝试使用
dist(dfAB,dfCD)
我收到错误:dist(dfAB,dfCD)中的错误:无效的距离方法
任何帮助深表感谢
dist(dfAB, dfCD)
波谷错误,因为的第二个参数dist()
是描述距离计算方法的字符串(例如“欧几里得”);dfAB
和dfCD
数据框中的坐标元组是字符串。因此,即使dist()
允许您计算两个数据帧的每个元素之间的距离,也会出错。我的方法不是很优雅,但是也许可以开始考虑如何处理数据了。
set.seed(60007561)
dat <- split(rpois(60, 2), paste0('df_', rep(letters[1:4], each = 15)))
for(i in names(dat)) {
assign(
i,
data.frame(split(dat[[i]], rep(letters[1:5], each = 3)))
)
}
# inspect the data
head(
do.call(
cbind,
lapply(
list(df_a, df_b, df_c, df_d),
cbind,
data.frame(' ' = rep(' ', 3), check.names = F)
)
)
)
# a b c d e a b c d e a b c d e a b c d e
# 1 1 2 1 2 3 0 2 1 2 1 5 0 2 2 0 2 5 2 3 3
# 2 5 0 2 0 3 2 5 1 2 3 0 0 4 2 2 3 1 1 1 2
# 3 3 2 1 3 0 4 2 0 2 2 0 3 1 2 0 2 2 5 1 4
做两个tibbles的列a...e
,其中每个列中包含的列的数据帧x, y
对应于从数据帧中的数据df_a
,df_b
; 和df_c
,df_d
分别。第一个合成的三角形对应于点,第二个斜度对应于点:
df_ab <- as_tibble(lapply(map2(df_a, df_b, ~ list(x = .x, y = .y)), as.data.frame))
df_cd <- as_tibble(lapply(map2(df_c, df_d, ~ list(x = .x, y = .y)), as.data.frame))
#df_ab
# # A tibble: 3 x 5
# a$x $y b$x $y c$x $y d$x $y e$x $y
# <int> <int> <int> <int> <int> <int> <int> <int> <int> <int>
# 1 1 0 2 2 1 1 2 2 3 1
# 2 5 2 0 5 2 1 0 2 3 3
# 3 3 4 2 2 1 0 3 2 0 2
#
#df_cd
# # A tibble: 3 x 5
# a$x $y b$x $y c$x $y d$x $y e$x $y
# <int> <int> <int> <int> <int> <int> <int> <int> <int> <int>
# 1 5 2 0 5 2 2 2 3 0 3
# 2 0 3 0 1 4 1 2 1 2 2
# 3 0 2 3 2 1 5 2 1 0 4
从计算之间的欧几里德距离从数据到数据:
distances <- map2_df(
df_ab,
df_cd,
~ sqrt((.x$x - .y$x)^2 + (.x$y - .y$y)^2)
)
#distances
# # A tibble: 3 x 5
# a b c d e
# <dbl> <dbl> <dbl> <dbl> <dbl>
# 1 4.47 3.61 1.41 1 3.61
# 2 5.10 4 2 2.24 1.41
# 3 3.61 1 5 1.41 2
请注意,上表表示从列a ... e的每个点到第一个表的距离,到第二个表中的相应点的距离
绘制列的距离a
(以验证方法或只是为了好玩):
sgms <- data.frame(
x = df_a$a,
y = df_b$a,
xend = df_c$a,
yend = df_d$a,
l = round(distances$a, 1)
) %>%
mutate(lx = (x + xend) / 2, ly = (y + yend) / 2)
ggplot(data = sgms, aes(x = x, y = y, xend = xend, yend = yend)) +
geom_segment(lty = 3, arrow = arrow(10, ,'closed', ends = 'last')) +
geom_label(aes(x = lx, y = ly, label = l)) +
geom_point(aes(x = x, y = y), pch = 21, size = 3.5) +
geom_text(aes(x = x, y = y, label = sprintf('(%d, %d)', x, y)), vjust = 2) +
geom_point(aes(x = xend, y = yend), pch = 22, size = 3.5) +
geom_text(aes(x = xend, y = yend, label = sprintf('[%d, %d]', xend, yend)), vjust = -2) +
expand_limits(y = c(-.5, 5.5), x = c(-.5, 5.5)) +
ggtitle('Distances btw df_ab, df_cd; col. a') +
ggthemes::theme_tufte()
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句