Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in R Programming by (5.3k points)

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:

[1] 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!

1 Answer

0 votes
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)

[1] 0 0 2 1 1 0 0

Browse Categories

...