I have two vectors
a <- c(1, 5, 2, 1, 2, 3, 3, 4, 5, 1, 2) b <- (1, 2, 3, 4, 5, 6)
I want to know how many times each element in b occur
You can do this using factor and table
factor
table
table(factor(a, unique(b))) # #1 2 3 4 5 6 #3 3 2 1 2 0
Since you mentioned match, here is a possibility without sapply loop (thanks to @thelatemail)
match
sapply
table(factor(match(a, b), unique(b))) # #1 2 3 4 5 6 #3 3 2 1 2 0