text: user14023: I have two data frames. One data frame with one column and second data frame with two columns. I need to merge first data frame with either column of the second data frame and returns the values. After merging the values,the order of the first column should be same as the input. Please find the sample input below
  first_column = c("ENSG00000165588","ENSG00000213551")
  df1 = data.frame(first_column)
  first_column = c("ENSG00000142192", "ENSG00000140575", "ENSG00000165588", "ENSG00000165588", "ENSG00000213551", "ENSG00000213551","ENSG00000197153")
  second_column = c("ENSG00000165588", "ENSG00000165588", "ENSG00000186908", "ENSG00000135446", "ENSG00000273983", "ENSG00000274267","ENSG00000213551")
  df2 = data.frame(first_column,second_column)
  
  Please find the output below.
  first_column = c("ENSG00000165588","ENSG00000165588","ENSG00000165588","ENSG00000165588","ENSG00000213551","ENSG00000213551","ENSG00000213551")
  second_column = c("ENSG00000142192","ENSG00000140575","ENSG00000186908","ENSG00000135446","ENSG00000273983","ENSG00000274267","ENSG00000197153")
  output = data.frame(first_column,second_column)
  
  
  user15931: I think this question is more about subsetting than merging. You can try this solution in R:
  first_column = c("ENSG00000165588","ENSG00000213551")
  df1 = data.frame(first_column)
  first_column = c("ENSG00000142192", "ENSG00000140575", "ENSG00000165588", "ENSG00000165588",
                   "ENSG00000213551", "ENSG00000213551","ENSG00000197153")
  second_column = c("ENSG00000165588", "ENSG00000165588", "ENSG00000186908", "ENSG00000135446",
                    "ENSG00000273983", "ENSG00000274267","ENSG00000213551")
  df2 = data.frame(first_column,second_column)
  
  output = rbind(df2[df2$first_column %in% df1$first_column,],
                 df2[df2$second_column %in% df1$first_column,])
  
  output <- transform(output, second_column = ifelse(output$second_column%in%df1$first_column,
                                                     first_column, second_column),
                              first_column = ifelse(output$second_column%in%df1$first_column,
                                                    second_column, first_column))
  output <- output[ order(row.names(output)), ]
  
  
original_text: 14023: <p>I have two data frames. One data frame with one column and second data frame with two columns. I need to merge first data frame with either column of the second data frame and returns the values. After merging the values,the order of the first column should be same as the input. Please find the sample input below</p>
  <pre><code>first_column = c(&quot;ENSG00000165588&quot;,&quot;ENSG00000213551&quot;)
  df1 = data.frame(first_column)
  first_column = c(&quot;ENSG00000142192&quot;, &quot;ENSG00000140575&quot;, &quot;ENSG00000165588&quot;, &quot;ENSG00000165588&quot;, &quot;ENSG00000213551&quot;, &quot;ENSG00000213551&quot;,&quot;ENSG00000197153&quot;)
  second_column = c(&quot;ENSG00000165588&quot;, &quot;ENSG00000165588&quot;, &quot;ENSG00000186908&quot;, &quot;ENSG00000135446&quot;, &quot;ENSG00000273983&quot;, &quot;ENSG00000274267&quot;,&quot;ENSG00000213551&quot;)
  df2 = data.frame(first_column,second_column)
  </code></pre>
  <p>Please find the output below.</p>
  <pre><code>first_column = c(&quot;ENSG00000165588&quot;,&quot;ENSG00000165588&quot;,&quot;ENSG00000165588&quot;,&quot;ENSG00000165588&quot;,&quot;ENSG00000213551&quot;,&quot;ENSG00000213551&quot;,&quot;ENSG00000213551&quot;)
  second_column = c(&quot;ENSG00000142192&quot;,&quot;ENSG00000140575&quot;,&quot;ENSG00000186908&quot;,&quot;ENSG00000135446&quot;,&quot;ENSG00000273983&quot;,&quot;ENSG00000274267&quot;,&quot;ENSG00000197153&quot;)
  output = data.frame(first_column,second_column)
  </code></pre>
   15931: <p>I think this question is more about subsetting than merging. You can try this solution in R:</p>
  <pre><code>first_column = c(&quot;ENSG00000165588&quot;,&quot;ENSG00000213551&quot;)
  df1 = data.frame(first_column)
  first_column = c(&quot;ENSG00000142192&quot;, &quot;ENSG00000140575&quot;, &quot;ENSG00000165588&quot;, &quot;ENSG00000165588&quot;,
                   &quot;ENSG00000213551&quot;, &quot;ENSG00000213551&quot;,&quot;ENSG00000197153&quot;)
  second_column = c(&quot;ENSG00000165588&quot;, &quot;ENSG00000165588&quot;, &quot;ENSG00000186908&quot;, &quot;ENSG00000135446&quot;,
                    &quot;ENSG00000273983&quot;, &quot;ENSG00000274267&quot;,&quot;ENSG00000213551&quot;)
  df2 = data.frame(first_column,second_column)
  
  output = rbind(df2[df2<span class="math-container">$first_column %in% df1$</span>first_column,],
                 df2[df2<span class="math-container">$second_column %in% df1$</span>first_column,])
  
  output &lt;- transform(output, second_column = ifelse(output<span class="math-container">$second_column%in%df1$</span>first_column,
                                                     first_column, second_column),
                              first_column = ifelse(output<span class="math-container">$second_column%in%df1$</span>first_column,
                                                    second_column, first_column))
  output &lt;- output[ order(row.names(output)), ]
  </code></pre>
  