title: If else statements to check if a string contains a substring in R
body: <p>I have a list that contains multiple strings for each observation (see below). </p>
  
  <pre><code>  [1] A, C, D 
    [2] P, O, E
    [3] W, E, W
    [4] S, B, W
  </code></pre>
  
  <p>I want to test if the strings contain certain substrings and if so, return the respective substring, in this example this would be either "A" or "B" (see desired outcome below). Each observation will only contain either one of the 2 substrings (A|B) </p>
  
  <pre><code>  [1] A 
    [2] NA
    [3] NA
    [4] B
  </code></pre>
  
  <p>No I have made this attempt in solving it, but it seems very inefficient and also I do not get it to work. How could I solve it? </p>
  
  <pre><code>  if (i == "A") {
      type &lt;- "A"
    } else if { (i == "B") 
      type &lt;- "B" 
    } else { type &lt;- "NA"
    } 
  </code></pre>
  
  <p>Note: I will need to loop it through > 1000 observations </p>