如何将列表中的每个元素与其他元素进行比较并将结果输出为 R 中的成对比较矩阵?

发布于 2025-01-11 05:01:29 字数 480 浏览 0 评论 0原文

我正在尝试自动化计算最近植被研究中调查的每一对可能的地点的杰卡德相似度指数的过程。

下面是我的数据格式的虚拟列表,其中 x、y 和 z 是离散的调查站点,以及函数 jaccard()

x <- c("sp1","sp2","sp3")
y <- c("sp2","sp3","sp4")
z <- c("sp3","sp4","sp5")
dummy_list <- list(x,y,z)

jaccard <- function(a, b) {
intersection = length(intersect(a, b))
union = length(a) + length(b) - intersection
return (intersection/union) }

我想将每个成对比较(xy、xz、yz)传递给 jaccard() 并输出计算出的 Jaccard 指数矩阵。我怎样才能实现这个目标?

I am trying to automate the process of calculating Jaccard's index of similarity for every possible pair of sites surveyed in a recent vegetation study.

Below is a dummy list in the format of my data, where x, y, and z are discrete survey sites, and function jaccard().

x <- c("sp1","sp2","sp3")
y <- c("sp2","sp3","sp4")
z <- c("sp3","sp4","sp5")
dummy_list <- list(x,y,z)

jaccard <- function(a, b) {
intersection = length(intersect(a, b))
union = length(a) + length(b) - intersection
return (intersection/union) }

I want to pass each pairwise comparison (x-y, x-z, y-z) to jaccard() and output a matrix of calculated Jaccard indicies. How can I achieve this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

倾城月光淡如水﹏ 2025-01-18 05:01:29

我们可以首先矢量化您的jaccard函数,然后使用outer

x <- c("sp1","sp2","sp3")
y <- c("sp2","sp3","sp4")
z <- c("sp3","sp4","sp5")

dummy_list <- setNames(list(x, y, z), c("x","y","z"))

jaccard <- function(a, b) {
  intersection = length(intersect(a, b))
  union = length(a) + length(b) - intersection
  return (intersection/union)
}

vjaccard <- Vectorize(jaccard)

outer(dummy_list, dummy_list, FUN = "vjaccard")
#>     x   y   z
#> x 1.0 0.5 0.2
#> y 0.5 1.0 0.5
#> z 0.2 0.5 1.0

创建于2022年3月2日,由reprex 包 (v2.0.1)

We could first Vectorize your jaccard function and then use outer:

x <- c("sp1","sp2","sp3")
y <- c("sp2","sp3","sp4")
z <- c("sp3","sp4","sp5")

dummy_list <- setNames(list(x, y, z), c("x","y","z"))

jaccard <- function(a, b) {
  intersection = length(intersect(a, b))
  union = length(a) + length(b) - intersection
  return (intersection/union)
}

vjaccard <- Vectorize(jaccard)

outer(dummy_list, dummy_list, FUN = "vjaccard")
#>     x   y   z
#> x 1.0 0.5 0.2
#> y 0.5 1.0 0.5
#> z 0.2 0.5 1.0

Created on 2022-03-02 by the reprex package (v2.0.1)

雨落□心尘 2025-01-18 05:01:29
jaccard <- function(List) {
  ln <- combn(List, 2,function(x){
    n <- length(intersect(x[[1]], x[[2]]))
    m <- length(unlist(x))
    n/(m-n)})
  structure(ln, Size = length(ln), Diag = FALSE, class = 'dist')
}

jaccard(dummy_list)
    1   2
2 0.5    
3 0.2 0.5
jaccard <- function(List) {
  ln <- combn(List, 2,function(x){
    n <- length(intersect(x[[1]], x[[2]]))
    m <- length(unlist(x))
    n/(m-n)})
  structure(ln, Size = length(ln), Diag = FALSE, class = 'dist')
}

jaccard(dummy_list)
    1   2
2 0.5    
3 0.2 0.5
往昔成烟 2025-01-18 05:01:29

我们可以使用以下基本 R 方法(不使用 jaccard 函数,但遵循相同的定义)

> dummy_list <- list(x = x, y = y, z = z)

> 1 / (outer(lengths(dummy_list), lengths(dummy_list), `+`) / crossprod(table(stack(dummy_list))) - 1)
    x   y   z
x 1.0 0.5 0.2
y 0.5 1.0 0.5
z 0.2 0.5 1.0

We can use the following base R approach (without using the jaccard function but following the same definition)

> dummy_list <- list(x = x, y = y, z = z)

> 1 / (outer(lengths(dummy_list), lengths(dummy_list), `+`) / crossprod(table(stack(dummy_list))) - 1)
    x   y   z
x 1.0 0.5 0.2
y 0.5 1.0 0.5
z 0.2 0.5 1.0
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文