Compare Occurrences Between 2 Dataframes In R
2
0
Entering edit mode
10.2 years ago
viniciushs88 ▴ 50

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 • 15k views
ADD COMMENT
0
Entering edit mode
ADD REPLY
4
Entering edit mode
10.2 years ago
seidel 11k

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.

ADD COMMENT
0
Entering edit mode

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

ADD REPLY
1
Entering edit mode
10.2 years ago
hardingnj ▴ 210

Something like:

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

Should work (untested)

ADD COMMENT

Login before adding your answer.

Traffic: 2241 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6