I can easily compare 2 vectors in R to see how many elements are the same. Let's say
a<- c(1,2,3,4)
b<- c(1,2,3,5)
sum(a==b) would give me what I want
But how can I compare 3 vectors? or more than 3 vectors at the same time?
a<- c(1,2,3,4)
b<- c(1,2,3,5)
c<- c(2,3,4,5)
sum(a==b & b==c)
I am looking for whether the elements are the same in the same position. In this case, this will give me zero, since a, b, c are not all the same in the same position.
count = 0
for(i in 1:length(a)){
if((a[i]==b[i]) & (a[i]==c[i]))
count=count+1
}
source
share