0

I have a list of vectors with character strings:

lst <- list(v1 = c("A", "B", "C", "D"), v2 = c("B", "C", "D", "E"), v3 = c("C", "D", "E", "F")

How can I get a new vector that includes only those character strings that intersect each vector. E.g. in this case

out <- c("C", "D")

A solution with lapply would be great.

pogibas
  • 25,773
  • 19
  • 74
  • 108
Antti
  • 1,212
  • 2
  • 13
  • 27

1 Answers1

4

The easiest function to use would be Reduce. Try

Reduce(intersect, lst)

That's basically the same as

# data
x <- list(A, B, C)

# these are equivalent
Reduce(intersect, x)
intersect(intersect(A, B), C)
MrFlick
  • 178,638
  • 15
  • 253
  • 268