Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find common elements in two vectors a and b where a=c("a", "b") and b=c("b", "c") using R? [duplicate]

Tags:

r

I need to find the common elements in vectors a & b and make a new vector c which contains these common elements, and afterward count the number of elements in c (i.e the number of common elements which in this case would be 1).

like image 267
Ishaan Bedi Avatar asked Oct 17 '25 14:10

Ishaan Bedi


1 Answers

Is this what you are looking for?

a <- LETTERS[c(1:5, 10:15)]
b <- LETTERS[1:10]

intersect(a,b)
[1] "A" "B" "C" "D" "E" "J"

length(intersect(a,b))
[1] 6
like image 104
Chris Ruehlemann Avatar answered Oct 20 '25 04:10

Chris Ruehlemann



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!