-2

Here's a figure to illustrate, since I couldn't reproduce with dput and dump. I tried using for() loop in combination with a match or %in%, but I just couldn't get through in time.

Image

I want to match ID from df1 with ACCESSION in df2, and then replace "NA" in df1 with GENE_SYMBOL from df2.

The values are not in the same order, and their columns have different sizes. Thus, I believe that I should use a combination of for loops + match or something.

T. Leal
  • 13
  • 3
  • Hi there. There's a general expectation amongst readers that question-askers show us what they have tried before asking a question. Would you edit whatever you have into your post please? While I am sure you have done your prior research, often people do not do this, and we have to draw the line somewhere, so we do not get known as a place where free work can be ordered. – halfer Apr 16 '17 at 22:19
  • Please don't post an image of your data, just use `dput`, copy, paste here, and (optionally) press `ctrl-k` to indent for code-formatting. Other good-question suggests can be found on SO's help page as [minimal/verifiable questions](http://stackoverflow.com/help/mcve) as well as a popular Q/A about [reproducible questions](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – r2evans Apr 16 '17 at 22:19
  • @halfer Sorry. Do you need me to add what I've tried before asking? If so, sure. – T. Leal Apr 16 '17 at 22:28
  • Yes please, that would be great. – halfer Apr 16 '17 at 22:28
  • @r2evans I tried dput and dump, but my object wasn't reproduced correctly. – T. Leal Apr 16 '17 at 22:41

1 Answers1

0

Looks like you need a left-join, something like:

merge(df1, df2, by.x = "ID", by.y = "ACCESSION", all.x = TRUE)

If you are using dplyr, you can do

library(dplyr)
left_join(df1, df2, by = c("ID" = "ACCESSION"))
r2evans
  • 141,215
  • 6
  • 77
  • 149