How to compare two dataframes and extract the matching entries as a row?
2
0
Entering edit mode
5.4 years ago
aksoyluinci ▴ 10

Hello all!

I have two data frames as the first one contains gene names (511 lines) and a single column and the second one contains Chromosome,Position,Rsid,Ref,ALT....Gene (187th column) and so on.

I want to compare these two dataframes and keep the rows in second dataframe based on "Gene" column. How can I do this in RStudio?

for example:

df1:

AARD
NOC2L
EPHA10

df2:

info.. info.. info... SAMD11
info.. info.. info..   NOC2L
info.. info.. info..   EPHA10
info.. info.. info..   AARD

new dataframe as output(the one I want):

info.. info.. info..   NOC2L
info.. info.. info..   EPHA10
info.. info.. info..   AARD

Thanks!

R data • 24k views
ADD COMMENT
1
Entering edit mode

What have you tried? This seems like a quite basic R subsetting/dataframe slicing question, of which you can find plenty of solutions online.

ADD REPLY
0
Entering edit mode

Well,i tried several methods,though i couldnt extract the whole row in dataframe 2, instead i could extract the names only + when i used the intersect function it gave me error since these datasets are not same in size.

ADD REPLY
2
Entering edit mode

Please share the code even if it is not working, that would help us to understand the problem.

i tried several methods

And, this is a basic R question, more suitable at StackOverflow, just merge common gene columns, see:

Or use %in%:

ADD REPLY
0
Entering edit mode

If it is single field matching, you can use join in *nix

ADD REPLY
0
Entering edit mode
5.4 years ago

You can try :

required_df <- df2[df2$genecolumn %in% df1$gene_list_column_name,]

As WouterDeCoster mentioned, from next time post your code and what was the error/result that you were getting.

ADD COMMENT

Login before adding your answer.

Traffic: 2519 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