This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Compare Occurrences Between 2 Dataframes In R

Hi. I would like to compare to a string frequence between two dataframes in R.

My first dataframe (X):

 List1
Engl001
Engl002
Engl003

My second dataframe (Y):

 List1    ram
Engl001   noi2
Engl001   oui5
Engl003   ki4

My expected output:

 List1    Count
 Engl001    2
 Engl002    0
 Engl003    1

Thank you!

r

2 answers

You really should cast this in some biological context to keep it relevant in this forum, but this is a problem I often encounter in terms of counting gene ids etc., and one solution would be to use the table function. Assuming the structure of your second data frame:

table(Y$List1)

would return the frequency count of elements the column called "List1". I don't really understand the purpose of your first, one-column data frame, but if the purpose was to narrow the scope of a second data frame (Y), you could do the following:

Y <- Y[Y$List1 %in% X$List1,]
table(Y$List1)

The first step uses the %in% operator to return a boolean vector reflecting if the elements of one thing are found in the other. This reduces your Y data frame to just the things found in X. Then you count the frequency of things in Y. The advantage of %in% over match() is that match() would find only the first occurence.

Agreed- stackoverflow or R help is probably a better choice for this kind of thing.

Something like:

table(factor(Y$List1,levels=unique(X$List1)))

Should work (untested)

Log in to answer this question.