假设我有以下两个数据框
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)中的错误:无效的距离方法
非常感谢您的帮助
答案 0 :(得分:1)
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
用a...e
列进行两个小标题,其中每一列包含数据列,其中x, y
列对应于数据帧df_a
,df_b
中的数据;分别为df_c
和df_d
。第一个产生的小节对应于 from points ,第二个小标题对应于 to points :
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()
答案 1 :(得分:1)
同意@utubun,在您的示例中使用dist
是一个问题。
dist
有助于计算矩阵中元素之间的距离。例如:
R> m1 <- matrix(1:8, nrow=4)
R> m1
[,1] [,2]
[1,] 1 5
[2,] 2 6
[3,] 3 7
[4,] 4 8
R> dist(m1)
1 2 3
2 1.414214
3 2.828427 1.414214
4 4.242641 2.828427 1.414214
请注意,行[1,]和行[2,]之间的欧式距离是1.4,这类似于(1,5)和(2,6)或sqrt(2)
的坐标之间的距离。
对于您而言,您不需要所有点之间的矩阵比较-您对两个矩阵中的坐标对之间的距离最感兴趣。
如@utubun所述,您需要为坐标提供数值。例如,您可以这样做:
mat1 <- matrix(apply(dfAB, 1:2, function(x) as.numeric(unlist(strsplit(x, ',')))), ncol = 2, byrow = T)
mat2 <- matrix(apply(dfCD, 1:2, function(x) as.numeric(unlist(strsplit(x, ',')))), ncol = 2, byrow = T)
这将为您提供两个分别具有2列的数值矩阵,可以将其视为您的坐标:
R> mat1[1:5,]
[,1] [,2]
[1,] 1 1
[2,] 3 2
[3,] 4 4
[4,] 1 5
[5,] 0 4
R> mat2[1:5,]
[,1] [,2]
[1,] 4 2
[2,] 3 2
[3,] 2 3
[4,] 4 0
[5,] 3 2
要获取距离,您可以创建一个简单的函数来计算欧式距离:
euclidean_distance <- function(p, q){
sqrt(sum((p - q)^2))
}
然后通过两个成对的坐标矩阵逐行调用该函数:
matrix(sapply(1:nrow(mat1), function(x) euclidean_distance(mat1[x,], mat2[x,])), ncol = 5, byrow = FALSE)
这将为您提供距离的最终矩阵:
[,1] [,2] [,3] [,4] [,5]
[1,] 3.162278 1.000000 4.472136 1.414214 1.414214
[2,] 0.000000 0.000000 2.236068 1.000000 2.000000
[3,] 2.236068 4.472136 5.385165 1.000000 1.000000
[4,] 5.830952 2.236068 4.242641 3.605551 3.605551
[5,] 3.605551 3.162278 1.000000 1.414214 2.000000
[6,] 2.828427 2.000000 2.000000 2.000000 2.236068
[7,] 1.414214 2.236068 2.236068 2.828427 1.414214
[8,] 1.000000 4.000000 2.828427 2.000000 2.000000
[9,] 3.000000 1.000000 1.000000 2.000000 1.000000
[10,] 2.236068 2.828427 4.123106 1.414214 1.000000
数据
set.seed(5)
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(.)))