0

Let's say I have these two lists, and I want to figure out the number of column elements among these two lists:

> xl
[[1]]
[1] 1 2 3 4 5

> yl
[[1]]
[1] 4 3 5 6 7

So, in this example, the answer would be 3. Any suggestions?

AstroCB
  • 12,337
  • 20
  • 57
  • 73
Vahid Mirjalili
  • 6,211
  • 15
  • 57
  • 80
  • 1
    Does each list `x1` and `y1` have exactly one element? (Why not make each of them a vector rather than a list with a vector in it- that is, `x = c(1, 2, 3, 4, 5)` rather than `x1 = list(c(1, 2, 3, 4, 5))`?) – David Robinson Oct 18 '13 at 02:13

1 Answers1

7

Use intersect:

x1 = list(c(1, 2, 3, 4, 5))
y1 = list(c(4, 3, 5, 6, 7))

length(intersect(x1[[1]], y1[[1]]))
# 3
David Robinson
  • 77,383
  • 16
  • 167
  • 187