Question: How to compare two dataframes and extract the matching entries as a row?
0
gravatar for aksoyluinci
7 months ago by
aksoyluinci10
aksoyluinci10 wrote:

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!

data R • 2.1k views
ADD COMMENTlink modified 7 months ago by martinaabner0940 • written 7 months ago by aksoyluinci10
1

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 REPLYlink written 7 months ago by WouterDeCoster39k

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 REPLYlink written 7 months ago by aksoyluinci10
2

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 REPLYlink modified 7 months ago • written 7 months ago by zx87547.5k

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

ADD REPLYlink written 7 months ago by cpad011211k
0
gravatar for kishorereddyanekalla
7 months ago by
United States
kishorereddyanekalla0 wrote:

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 COMMENTlink modified 7 months ago by WouterDeCoster39k • written 7 months ago by kishorereddyanekalla0
Please log in to add an answer.

Help
Access

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 1465 users visited in the last hour