user3237820
user3237820

Reputation: 211

Find frequencies of unique values of one vector in a a different vector

If I have a vector of observed values X and a vector of reference values Y, how do I use R to find the frequencies of each value of Y in X?

# create X and Y
X = c(1,2,4,5,1,4)
Y = 1:6

# desired output
Y  X 
1  2 
2  1
3  0
4  2 
5  1
6  0

I know how to find the frequencies of values of X, or what values of Y are in X, but this is proving (emrbarrinsgly) difficult. I apologise if this has been asked before but I am struggling to find similar questions.

I have tried

# 'count' in the "plyr" package
count(X , "unique(Y)" )

...but this returns:

unique.Y. freq
1         1
2         1
3         1
4         1
5         1
6         1

Thanks!

Upvotes: 1

Views: 213

Answers (1)

akrun
akrun

Reputation: 887098

We convert the 'X' to factor class specifying the levels as the unique elements of 'Y' (In this case, there are only 6 unique elements. But, if there are duplicate elements, use , levels= unique(Y)). Get the frequency of 'Y' and transformed 'X' and do the colSums.

colSums(table(Y,factor(X, levels=Y)))
#  1 2 3 4 5 6 
#  2 1 0 2 1 0 

Or as @docendodiscmus mentioned, we can apply table on the transformed 'X' to get the output (using this example)

table(factor(X, levels = Y))

Or use xtabs. By default, it gives the sum of duplicate elements. Here, we convert the 'Y' to logical vector so that we get automatically the frequency (by doing the sum) with xtabs.

xtabs(as.logical(Y)~factor(X, levels=Y))

Upvotes: 3

Related Questions