This is a test version of Biostars. For the public version, visit https://www.biostars.org.
How to compare two dataframes and extract the matching entries as a row?

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

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

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.

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

2 answers

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.

Really Nice article for refreshing basic data frame concepts and for beginners to start with R data structures. for more information visit at- https://avastsupportnumber.co.uk/blog/fix-avast-error-7005/

Log in to answer this question.