title: In R, select elements from a column if they occupy the same row as elements of another column
body: <p>This question has likely been asked elsewhere, but since I'm new to R and my vocabulary is limited, I can't seem to find it.</p>
  
  <p>If I have a character-filled data frame, say:</p>
  
  <pre><code>c1 &lt;- c("aa", "bb", "cc", "dd", "ee")
  c2 &lt;- c("ff", "gg", "hh", "ii", "jj")
  c3 &lt;- c("kk", "ll", "mm", "nn", "oo")
  c4 &lt;- c("pp", "qq", "rr", "ss", "tt")
  c5 &lt;- c("uu", "vv", "ww", "xx", "yy")
  df &lt;- cbind.data.frame(c1, c2, c3, c4, c5)
  </code></pre>
  
  <p>resulting in</p>
  
  <pre><code>  c1 c2 c3 c4 c5
  1 aa ff kk pp uu
  2 bb gg ll qq vv
  3 cc hh mm rr ww
  4 dd ii nn ss xx
  5 ee jj oo tt yy
  </code></pre>
  
  <p>And I want to select elements out of column 4 that occupy the same row as multiple elements out of column 2 (say, "gg", "ii", and "jj"), is there a way to do this without using a different command for each one?</p>
  
  <p>For example,</p>
  
  <pre><code>df$c4[i = c(2,4,5)]
  </code></pre>
  
  <p>will return</p>
  
  <pre><code>[1] qq ss tt
  Levels: pp qq rr ss tt
  </code></pre>
  
  <p>and</p>
  
  <pre><code>df$c4[df$c2 == "gg"]
  </code></pre>
  
  <p>will return</p>
  
  <pre><code>[1] qq
  Levels: pp qq rr ss tt
  </code></pre>
  
  <p>but when I try to combine the two with</p>
  
  <pre><code>df$c4[i = c(df$c2=="gg", df$c2=="ii", df$c2=="jj")]
  </code></pre>
  
  <p>R seems to only partially know what to do, returning</p>
  
  <pre><code>[1] qq   &lt;NA&gt; &lt;NA&gt;
  Levels: pp qq rr ss tt
  </code></pre>