0

I have a data frame df1 that looks like this:

id          name     type                                           
46        hsa:4052    gene 
50        hsa:7057    gene 
52        hsa:8454    gene 

And then I have a second data frame df2 that looks like this:

entry1    entry2      type       name        value
46         44        PPrel      activation    -->
52         50        PPrel      activation    -->
29         27        PPrel      activation    -->

Now the common columns in the two data frames are id from df1 and entry1 and entry2 from df2. What i want my results to look like is this:

entry1    entry2      type       name        value
hsa:4052   44        PPrel      activation    -->
hsa:8454  hsa:7057   PPrel      activation    -->
29         27        PPrel      activation    --> 

So what i want basically is replace the entry1 and entry2 ids in df2 with the names of corresponding ids from df1. I tried merge but obviously merge doesn't help here. Any help?

nadizan
  • 1,323
  • 10
  • 23

1 Answers1

2

Try this:

df2[,1:2]<-lapply(df2[,1:2],
           function(x) ifelse(x %in% df1$id,df1$name[match(x,df1$id)],x))
#    entry1   entry2  type       name value
#1 hsa:4052       44 PPrel activation   -->
#2 hsa:8454 hsa:7057 PPrel activation   -->
#3       29       27 PPrel activation   -->
nicola
  • 24,005
  • 3
  • 35
  • 56