【问题标题】:Calculate pairwise Jaccard index between character vectors in a list计算列表中字符向量之间的成对 Jaccard 索引
【发布时间】:2018-07-06 03:57:44
【问题描述】:

我有以下格式的字符向量

char1 <- c(“Hello”, “was”, “this”, “is”, “that”, “Boston”, “San”, “Francisco”)
char2 <- c(“John”, “was”, “they”, “is”, “Hello”, “Boston”, “San”, “Diego”)
char3 <- c(“John”, “very”, “happens”, “is”, “Hello”, “has”, “San”, “Diego”)

list <- list(char1, char2, char3)

但是,我有大约 500 个,每个长度为 100,000。

如何计算此列表中所有向量的成对 Jaccard 索引(相似性度量)并将其作为数据框输出(NA 用于比较相同的字符向量)?这样做最有效的方法是什么?

谢谢!

【问题讨论】:

  • stringdist::stringdist

标签: r


【解决方案1】:

您可以尝试以下方法来获得unionintersectdplyr 中的所有成对距离

dist <- unlist(lapply(combn(list, 2, simplify = FALSE), function(x) {
  length(intersect(x[[1]], x[[2]]))/length(union(x[[1]], x[[2]])) }))

dist
[1] 0.4545455 0.2307692 0.4545455

要查看哪些对与哪些值相关联,您可以添加索引:

cbind(t(combn(3,2)), dist)

              dist
[1,] 1 2 0.4545455
[2,] 1 3 0.2307692
[3,] 2 3 0.4545455

【讨论】:

    猜你喜欢
    • 2017-04-09
    • 2021-10-25
    • 2019-11-10
    • 1970-01-01
    • 2018-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多