用于计算两组点之间的平均连杆距离的函数

时间:2016-01-20 20:57:21

标签: r

hclust中,可以指定method = "average"在群集中使用平均关联。

我的情况是我有两个固定的集群,我想计算这两个集群之间的平均关联。

在R中有没有这样做的功能? hclust似乎使用Fortran代码来执行此操作。

示例数据:

structure(list(lon = c(106.0081819, 106.0621591, 106.0787142, 
105.9581624, 105.9982149, 105.9455287, 106.0726373, 106.12575, 
106.1110501, 106.060344, 106.0635147, 105.9575665, 105.9494248, 
106.0475363, 105.9564829, 105.9964291, 106.1037006, 105.9964291, 
106.1639749, 106.1110501), lat = c(21.1400879, 21.1766814, 21.1738006, 
21.202957, 21.1244525, 21.1101074, 21.1861204, 21.163438, 21.121444, 
21.169068, 21.1815923, 21.1085185, 21.0994022, 21.1688445, 21.1158848, 
21.1122605, 21.1988765, 21.1122605, 21.0178933, 21.121444), group = c("domestic", 
"foreign", "domestic", "domestic", "foreign", "domestic", "domestic", 
"foreign", "domestic", "domestic", "domestic", "domestic", "domestic", 
"domestic", "foreign", "domestic", "domestic", "foreign", "domestic", 
"domestic")), .Names = c("lon", "lat", "group"), class = c("tbl_df", 
"data.frame"), row.names = c(NA, -20L))

1 个答案:

答案 0 :(得分:1)

也许

d <- dist(df[, 1:2]) 
idx <- as.matrix(expand.grid(which(df$group=="domestic"), which(df$group=="foreign"))) 
mean(as.matrix(d)[idx])
# [1] 0.09028491

如果平均连锁是第1组中每个点与第2组中每个点之间的平均距离(此处为:欧几里德)。

相关问题