title: What does the error "the condition has length > 1 and only the first element will be used" mean?
body: <p>Here is my data set:</p>
  
  <pre><code>FullName &lt;- c("Jimmy John Cephus", "Frank Chester", "Hank Chester", "Brody Buck Clyde", "Merle Rufus Roscoe Jed Quaid")
  df &lt;- data.frame(FullName)
  </code></pre>
  
  <p>Goal: Look into FullName for any spaces, " ", and extract out the FirstName.</p>
  
  <p>My first step is to utilize the stringr library because I will utilize the str_count() and word() functions.</p>
  
  <p>Next I test <code>stringr::str_count(df$FullName, " ")</code> against the df and R returns:</p>
  
  <pre><code>[1] 2 1 1 2 4
  </code></pre>
  
  <p>This is what I expect.</p>
  
  <p>Next I test the word() function:</p>
  
  <pre><code>stringr::word(df$FullName, 1)
  </code></pre>
  
  <p>R returns:</p>
  
  <pre><code>[1] "Jimmy" "Frank" "Hank"  "Brody" "Merle"
  </code></pre>
  
  <p>Again, this is what I expect.</p>
  
  <p>Next I construct a simple UDF (user defined function) that incorporates the str_count() function:</p>
  
  <pre><code>split_firstname = function(full_name){
    x &lt;- stringr::str_count(full_name, " ")
    return(x)
  }
  split_firstname(df$FullName)
  </code></pre>
  
  <p>Again, R provides what I expect:</p>
  
  <pre><code>[1] 2 1 1 2 4
  </code></pre>
  
  <p>As a final step, I incorporate the word() function into the UDF and code for all of the conditions:</p>
  
  <pre><code>    split_firstname = function(full_name){
    x &lt;- stringr::str_count(full_name, " ")
    if(x==1){
      return(stringr::word(full_name,1))
    }else if(x==2){
      return(paste(stringr::word(full_name,1), stringr::word(full_name,2), sep = " "))
    }else if(x==4){
      return(paste(stringr::word(full_name,1), stringr::word(full_name,2), stringr::word(full_name,3), stringr::word(full_name,4), sep = " "))
    }
  }
  </code></pre>
  
  <p>Then I call the UDF and pass to it the FullName from the df:</p>
  
  <pre><code>split_firstname(df$FullName)
  </code></pre>
  
  <p>This time I did NOT get what I expected, R returned:</p>
  
  <pre><code>[1] "Jimmy John"    "Frank Chester" "Hank Chester"  "Brody Buck"    "Merle Rufus"  
  Warning messages:
  1: In if (x == 1) { :
    the condition has length &gt; 1 and only the first element will be used
  2: In if (x == 2) { :
    the condition has length &gt; 1 and only the first element will be used
  </code></pre>
  
  <p>I had expected R to return to me the following:</p>
  
  <pre><code>"Jimmy John", "Frank", "Hank", "Brody Buck", "Merle Rufus Roscoe Jed"
  </code></pre>