title: using anonymous functions in R with multiple arguments
body: <p>I'm attempting to generate new variables in a data frame that are conditional on two (or more) other variables in the data frame. I believe that the looping functions in R (i.e., lapply, sapply, etc.) are useful and efficient for this purpose. Something is not right, however, with my approach, and I can't figure out what.</p>
  
  <pre><code>M &lt;- data.frame(x=c("A", "A", "B", "B"), y=c(1,2,1,2))
  </code></pre>
  
  <p>Using this data frame, I would like to generate a new column z, containing logicals that are TRUE iff both <code>x == "A"</code> and <code>y == 1</code>. The following code is the best I can come up with here, but only seems to evaluate my first condition. </p>
  
  <pre><code>M$z &lt;- sapply(M$x, function(x,y) if((x == "A") &amp;&amp; (y == 1)) T else F, M$y)
  </code></pre>
  
  <ul>
  <li>Can this code be fixed for my purpose?</li>
  <li>Is there a better way of doing this in R, perhaps using other looping functions?</li>
  </ul>