0

I need to count the number of elements in vector x which equal any vector element in vector y.

From here I can count the number of elements in vector x which equal to value in scalar `y with code:

x <- c(4,23,4,23,5,43,54,56,657,67,67,435,
             453,435,324,34,456,56,567,65,34,435, 675)

sum(x == 67)

Question: How to count the number of elements in vector x which equal any vector element in vector y?

vasili111
  • 5,040
  • 8
  • 40
  • 72

2 Answers2

1

Try length-which-in:

length(which(x %in% y))
SmokeyShakers
  • 3,172
  • 1
  • 6
  • 17
1

You can use:

For All matches use this:

length(na.omit(match(x,y)))

For unique Matches use this:

length(intersect(x,y))
Rushabh Patel
  • 2,572
  • 11
  • 31