Good morning everyone,
list1 <- c("hola1", "hola2", "hola3")
list2 <- c("hola1", "hola2", "hola4")
list3 <- c("hola2", "hola7", "hola8")
list4 <- c("hola1", "hola7", "hola8")
You can use intersect
in combination with combn
. intersect
looks for common elements and combn
will apply it to all 1vs1 combinations of your lists:
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"
You are looking for intersect
:
> intersect( list1, list2 )
[1] "hola1" "hola2"
First put the character vectors in a list. Then extract common names in all lists with Reduce() and intersect().
x <- list(c("hola1", "hola2", "hola3"),
c("hola1", "hola2", "hola4"),
c("hola2", "hola7", "hola8"),
c("hola1", "hola7", "hola8"))
Reduct(intersect, x)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.