【问题标题】:Distance matrix to reshaped vector of distances sorted by positions距离矩阵到按位置排序的距离重组向量
【发布时间】:2019-05-30 08:35:05
【问题描述】:

我有一个5项的数据框,如下:

df = structure(list(item1 = c(1, 1, 1, 1, 2, 2, 2, 3, 3, 4), item2 = c(0, 
2, 3, 4, 0, 3, 4, 0, 4, 0)), row.names = c(NA, -10L), class = c("tbl_df", 
"tbl", "data.frame"))

另外,我还有项目之间的距离矩阵:

Dist1 = structure(c(0, 1.0919530596119, 1.09195161858136, 1.0919463791331, 
1.09194754111203, 1.0919530596119, 0, 1.7831197560388, 1.78314749640301, 
1.78315668532962, 1.09195161858136, 1.7831197560388, 0, 1.78315765983813, 
1.78314839437957, 1.0919463791331, 1.78314749640301, 1.78315765983813, 
0, 1.78314787222978, 1.09194754111203, 1.78315668532962, 1.78314839437957, 
1.78314787222978, 0), .Dim = c(5L, 5L), .Dimnames = list(c("1", 
"2", "3", "4", "5"), c("1", "2", "3", "4", "5")))

我想向df 添加第三个列,它将包含从Dist1 中提取的距离。它们必须与 df 中的索引指定的顺序相同,不包含 self dist 等。

现在,这几乎是Dist1 的下三角形,但不完全是。 (另请注意,Dist1 中的项目是 df 的项目标识的 1+)。

所以,预期的输出是:

df$Distances = c(1.091953, 1.783120, 1.783147, 1.783157, 1.091952, 1.783158, 
1.783148, 1.091946, 1.783148, 1.091948)

我怎样才能有效地提取这个(实际的数据结构要大得多)?

【问题讨论】:

  • 第一个值是Dist1[2, 1]还是第二个是Dist1[3, 2]
  • @RonakShah df 值的顺序只是从外部给出的。目的是将距离矩阵输出的顺序与df的顺序相匹配

标签: r euclidean-distance


【解决方案1】:

我认为这就是你想要做的事情

# Logic
df <- df %>% 
  group_by(item1, item2) %>% 
  mutate(Distance = Dist1[(item1)*5 + (item2 + 1)])

# Result
df
# A tibble: 10 x 3
# Groups:   item1, item2 [10]
   item1 item2 Distance
   <dbl> <dbl>    <dbl>
 1     1     0     1.09
 2     1     2     1.78
 3     1     3     1.78
 4     1     4     1.78
 5     2     0     1.09
 6     2     3     1.78
 7     2     4     1.78
 8     3     0     1.09
 9     3     4     1.78
10     4     0     1.09

df$Distance
 [1] 1.091953 1.783120 1.783147 1.783157 1.091952 1.783158 1.783148 1.091946
 [9] 1.783148 1.091948

【讨论】:

  • 仍然不清楚逻辑,但您可以直接通过 Dist1[(df$item1)*5 + (df$item2 + 1)] 获得相同的输出。
  • 是的,逻辑是一样的,@Omry Atia提到了lower triangle,所以我把它分组以确保没有重复。
猜你喜欢
  • 2016-08-23
  • 1970-01-01
  • 2016-12-06
  • 1970-01-01
  • 2011-08-08
  • 2016-05-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多