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
  • 27,303
  • 19
  • 84
  • 117
Antti
  • 1,263
  • 2
  • 16
  • 28

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
  • 195,160
  • 17
  • 277
  • 295