gpt4 book ai didi

r - 提取多个列表中的常用名称

转载 作者:行者123 更新时间:2023-12-02 07:30:10 25 4
gpt4 key购买 nike

大家早上好

我有四个“字符”类列表:

list1 <- c("hola1", "hola2", "hola3")
list2 <- c("hola1", "hola2", "hola4")
list3 <- c("hola2", "hola7", "hola8")
list4 <- c("hola1", "hola7", "hola8")

我想获得这些列表之间有多少项是共同的,就像维恩图一样,所以输出:

Between list1 and list2: hola1 and hola2 are common names. Between list1 and list3: hola2 is a common name. Among list1, list2, and list4: hola1 is a common name.

或者换句话说,类似的东西:

enter image description here

是这样的。你知道任何包或获取方法吗?

提前致谢。

最佳答案

您可以将intersectcombn 结合使用。 intersect 查找公共(public)元素,combn 会将其应用于列表的所有 1vs1 组合:

l <- list(c("hola1", "hola2", "hola3"),
c("hola1", "hola2", "hola4"),
c("hola2", "hola7", "hola8"),
c("hola1", "hola7", "hola8"))

combn(length(l), 2, FUN=function(i)intersect(l[[i[1]]], l[[i[2]]]),
simplify=FALSE)
# [[1]]
# [1] "hola1" "hola2"
#[[2]]
#[1] "hola2"
#[[3]]
#[1] "hola1"
#[[4]]
#[1] "hola2"
#[[5]]
#[1] "hola1"
#[[6]]
#[1] "hola7" "hola8"

关于r - 提取多个列表中的常用名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22807575/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com