2 views

This is hard for me to explain, so I will just give an example instead. I have two vectors below (a & b).

a <- c("cat","dog","banana","yogurt","dog")

b <- c("salamander","worm","dog","banana","cat","yellow","blue")

What I would like is the following results:

 0 0 2 1 1 0 0

where each element of the result is the number of times each element of b appears in the vector a.

do.call("c",lapply(b,function(x){sum(x == a)}))

This gives me what I want, but I need a vectorized/faster version of this because I am working with >20,000 records. Any help appreciated!

by

For a faster solution, you can use the match function to know the matching elements and table function to print the counts.i.e.,

a <- c("cat","dog","banana","yogurt","dog")

b <- c("salamander","worm","dog","banana","cat","yellow","blue")

result <- table(factor(b, levels=b)[match(a, b, nomatch=0)])

Output:

salamander   worm    dog   banana   cat     yellow       blue

0       0     2         1     1          0          0

To convert the output to a vector:

as.vector(result)

 0 0 2 1 1 0 0