I have a vector with characters like this:
v1 <- c("A", "B", "C", "D", "E")
and I want to count the frequencies of every character in another vector i.e.:
v2 <- c("B", "B", "C", "D", "C")
the output should be like this:
A B C D E
0 2 2 1 0
I've tried with table()
, merge()
, and using %in%
, but I can't get an output with also the zero frequencies of the characters which are not in the vector that I'm examining.
We can use table
after converting 'v2' to factor
with levels
specified as 'v1'
table(factor(v2, levels = v1))
# A B C D E
# 0 2 2 1 0