text: user14023: I have a data frame (named as df) with single column of protein IDs and other file with two columns of protein IDs (named as df1). I need to find the df IDs present in either first or second column of df1 and extract corresponding Id from the df1.
  df = data.frame(p = c("ENSG00000198400","ENSG00000066044","ENSG00000188612","ENSG00000177606","ENSG00000146648"))
  df1 = data.frame(p1 = c("ENSG00000105647","ENSG00000110395","ENSG00000165119","ENSG00000160293","ENSG00000198400","ENSG00000198400"),p2 = c("ENSG00000198400","ENSG00000198400","ENSG00000198400","ENSG00000142949","ENSG00000112936","ENSG00000162434"))
  
  Sample output:
  df2= data.frame(p = c("ENSG00000198400","ENSG00000198400","ENSG00000198400","ENSG00000198400","ENSG00000198400"),p2 = c("ENSG00000105647","ENSG00000110395","ENSG00000165119","ENSG00000112936","ENSG00000162434"))
  
  
  user15931: you can try this:
  df = data.frame(p = c("ENSG00000198400","ENSG00000066044","ENSG00000188612",
                         "ENSG00000177606","ENSG00000146648"))
  df1 = data.frame(p1 = c("ENSG00000105647","ENSG00000110395","ENSG00000165119",
                           "ENSG00000160293","ENSG00000198400","ENSG00000198400"),
                   p2 = c("ENSG00000198400","ENSG00000198400","ENSG00000198400",
                           "ENSG00000142949","ENSG00000112936","ENSG00000162434"))
  
  df2 = rbind(df1[df1$p1 %in% df$p,],df1[df1$p2 %in% df$p,])
  
  df2 <- transform(df2, p2 = ifelse(df2$p2%in%df$p, p1, p2), p1 = ifelse(df2$p2%in%df$p, p2, p1))
  
  
original_text: 14023: <p>I have a data frame (named as df) with single column of protein IDs and other file with two columns of protein IDs (named as df1). I need to find the df IDs present in either first or second column of df1 and extract corresponding Id from the df1.</p>
  <pre><code>df = data.frame(p = c(&quot;ENSG00000198400&quot;,&quot;ENSG00000066044&quot;,&quot;ENSG00000188612&quot;,&quot;ENSG00000177606&quot;,&quot;ENSG00000146648&quot;))
  df1 = data.frame(p1 = c(&quot;ENSG00000105647&quot;,&quot;ENSG00000110395&quot;,&quot;ENSG00000165119&quot;,&quot;ENSG00000160293&quot;,&quot;ENSG00000198400&quot;,&quot;ENSG00000198400&quot;),p2 = c(&quot;ENSG00000198400&quot;,&quot;ENSG00000198400&quot;,&quot;ENSG00000198400&quot;,&quot;ENSG00000142949&quot;,&quot;ENSG00000112936&quot;,&quot;ENSG00000162434&quot;))
  </code></pre>
  <p>Sample output:</p>
  <pre><code>df2= data.frame(p = c(&quot;ENSG00000198400&quot;,&quot;ENSG00000198400&quot;,&quot;ENSG00000198400&quot;,&quot;ENSG00000198400&quot;,&quot;ENSG00000198400&quot;),p2 = c(&quot;ENSG00000105647&quot;,&quot;ENSG00000110395&quot;,&quot;ENSG00000165119&quot;,&quot;ENSG00000112936&quot;,&quot;ENSG00000162434&quot;))
  </code></pre>
   15931: <p>you can try this:</p>
  <pre><code>df = data.frame(p = c(&quot;ENSG00000198400&quot;,&quot;ENSG00000066044&quot;,&quot;ENSG00000188612&quot;,
                         &quot;ENSG00000177606&quot;,&quot;ENSG00000146648&quot;))
  df1 = data.frame(p1 = c(&quot;ENSG00000105647&quot;,&quot;ENSG00000110395&quot;,&quot;ENSG00000165119&quot;,
                           &quot;ENSG00000160293&quot;,&quot;ENSG00000198400&quot;,&quot;ENSG00000198400&quot;),
                   p2 = c(&quot;ENSG00000198400&quot;,&quot;ENSG00000198400&quot;,&quot;ENSG00000198400&quot;,
                           &quot;ENSG00000142949&quot;,&quot;ENSG00000112936&quot;,&quot;ENSG00000162434&quot;))
  
  df2 = rbind(df1[df1<span class="math-container">$p1 %in% df$</span>p,],df1[df1<span class="math-container">$p2 %in% df$</span>p,])
  
  df2 &lt;- transform(df2, p2 = ifelse(df2<span class="math-container">$p2%in%df$</span>p, p1, p2), p1 = ifelse(df2<span class="math-container">$p2%in%df$</span>p, p2, p1))
  </code></pre>
  