title: R: Match Data Frame Elements with Vector Elements
body: <p>I'm working with a large data frame of patient data containing ICD codes, and I want to see if any codes in the list match those from a predefined list. I'd prefer the results of the test to output into my dataframe as a new column. Ideally I'd like to go from something like this:</p>
  
  <pre><code>dx1   dx2   dx3  ...  dx15
  4210  3359  2214      8178
  V0832 12218 7423      2294
  6463  3438  3243      1129
  </code></pre>
  
  <p>To something like this:</p>
  
  <pre><code>dx1   dx2   dx3  ...  dx15  ENDO
  4210  3359  4211      8178  TRUE
  V0832 12218 7423      2294  TRUE
  6463  3438  E3243      1129  FALSE
  </code></pre>
  
  <p>I created a vector with my codes of interest:</p>
  
  <pre><code>ICD9EndoCodes &lt;-c("4210","4211","4219","4249","421","11281")
  </code></pre>
  
  <p>But haven't been able to figure out how to perform the logical test on element of my data frame and output a single column. The closest I've come is:</p>
  
  <pre><code>x &lt;-ifelse(df_04_13[,4:18] == "4210", TRUE, FALSE)
  </code></pre>
  
  <p>...which creates a matrix "x" with the true and false correctly assigned for each position in the matrix. But when I've taken a stab at generalizing it...</p>
  
  <pre><code>x &lt;- for(i in 1:length(ICD9EndoCodes)) 
       ifelse(df_04_13[,4:18] == ICD9EndoCodes[i], TRUE, FALSE)
  </code></pre>
  
  <p>I get "null". I feel like I'm overlooking an obvious, straightforward way to go about this, but my unfamiliarity with the environment has left me struggling.</p>