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
  • 6,032
  • 10
  • 50
  • 80

2 Answers2

1

Try length-which-in:

length(which(x %in% y))
SmokeyShakers
  • 3,372
  • 1
  • 7
  • 18
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,672
  • 13
  • 34