Skip to content

Commit

Permalink
Add Jaccard index to columns returned
Browse files Browse the repository at this point in the history
  • Loading branch information
jcalendo committed Sep 25, 2024
1 parent 43bda5c commit 6fd6f4a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
14 changes: 9 additions & 5 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -282,8 +282,8 @@ remove_var.SummarizedExperiment <- function(x, p, assay = "counts") {
#' @param x List of vectors to perform intersections on
#' @return data.table
#' @returns a data.table containing columns for the sets being compared, a list column
#' which contains the actual values in the intersection, and a column with the
#' intersection size.
#' which contains the actual values in the intersection, a column with the
#' intersection size, and a column with the Jaccard index.
#' @export
#' @examples
#' l <- list(
Expand All @@ -310,22 +310,26 @@ pairwise_intersections <- function(x) {
s1 <- vector("character", n_pairs)
s2 <- vector("character", n_pairs)
intersection <- vector("list", n_pairs)
jaccard <- vector("numeric", n_pairs)

idx <- 1
for (i in 1:(n - 1)) {
for (j in (i + 1):n) {
s1[[idx]] <- lnames[i]
s2[[idx]] <- lnames[j]
intersection[[idx]] <- intersect(x[[i]], x[[j]])
int <- intersection[[idx]] <- intersect(x[[i]], x[[j]])
u <- union(x[[i]], x[[j]])
jaccard[[idx]] <- length(int) / length(u)
idx <- idx + 1
}
}

data.table::data.table(
Set1 = s1,
Set2 = s2,
Intersection = intersection,
Elements = vapply(intersection, length, numeric(length = 1L))
Elements = vapply(intersection, length, numeric(length = 1L)),
Jaccard = jaccard,
Intersection = intersection
)
}

Expand Down
4 changes: 2 additions & 2 deletions man/pairwise_intersections.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 6fd6f4a

Please sign in to comment.