R 计算列表中字符向量之间的成对索引
我有以下格式的字符向量R 计算列表中字符向量之间的成对索引,r,R,我有以下格式的字符向量 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”)
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)
char1您可以在dplyr
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
查看stringdist::stringdist
cbind(t(combn(3,2)), dist)
dist
[1,] 1 2 0.4545455
[2,] 1 3 0.2307692
[3,] 2 3 0.4545455