title: Use if statement in apply to create new variable
body: <p>I have a data.frame with ID number, Sex and age. I want to create column with a score based on sex and age. First I want to assign a score of 1 to all males, <code>M</code></p>
  
  <pre><code>ID   Sex   Age   sex_score
  1    M     72    1
  2    M     65    1
  3    F     55    0
  </code></pre>
  
  <p>I have tried with both a <code>for</code> loop and with <code>sapply</code> but I'm still a beginner and don't really know how to use these. These are my attempts:</p>
  
  <p><code>sex_score &lt;- for (i in 1:nrow(df)) {if (df$Sex == "M") {1} else {0}}</code>
  I get the warnings </p>
  
  <pre><code>In if (eligible$Sex == "M") {... :
  the condition has length &gt; 1 and only the first element will be used 
  </code></pre>
  
  <p>I've also tried
  <code>Sex_score &lt;-  sapply(df,function(x)if (df$Sex == "M") {1} else {0})</code>
  I get the same warnings.</p>