title: How to write an R function replace a TRUE with "X" and a FALSE with "Y"
body: <p>I am currently trying to write a function that assesses if the character variable has an &quot;F&quot; or an &quot;M&quot; inside the character that has multiple numbers as well (ie. 3849F is ID 3849, Female). I have sorted out a function to give me a boolean output that gives TRUE is F is present and FALSE is M is present.</p>
  <p>How can I now use this boolean output to replace these with the desired &quot;F&quot; and &quot;M&quot; character values?</p>
  <p>The code I have thus far is below:</p>
  <pre><code>sex = list()    
  for (i in 1:length(dataset)){
        fsex = grepl(&quot;F&quot;, dataset, fixed = T)
        if (fsex == T) sex = c(sex, &quot;F&quot;)
        else if (fsex == F) sex = c(sex, &quot;M&quot;)
      }
  </code></pre>
  <p><img src="https://i.stack.imgur.com/z6knu.png" alt="the original dataset" /></p>
  <p><img src="https://i.stack.imgur.com/b5zxa.png" alt="what I would like the output to look like" /></p>
  <p><img src="https://i.stack.imgur.com/EeIQq.png" alt="fsex output- I desire each TRUE to be replaced with &quot;F&quot; and each FALSE to be replaced with &quot;M&quot;" /></p>