title: Extract last word in string in R - error faced
body: <p>First, I wish to extract the last word and first word for the <code>Description</code> column (this column contains at least 3 words) into a newly created column <code>firstword</code> and <code>lastword</code>. However, the word() function is not applied to all the rows. As such, there are many rows with empty <code>lastword</code>, though these rows actually have a last word (as you can see from the <code>Description</code> column). This is shown in the first two lines of codes.</p>
  
  <p>Second, I am also trying to get the third line of code to replace the <code>lastword</code> with <code>firstword</code>, if <code>lastword</code> is empty. However it isn't working.</p>
  
  <p>Is there a way to rectify this?</p>
  
  <pre><code>c1$lastword = word(c1$Description,start=-1) #extract last word
  c1$firstword = word(c1$Description,start=1) #extract first word
  c1$lastword=ifelse(c1$lastword == " ", c1$firstword, c1$lastword)
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/KTYJ5.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/KTYJ5.png" alt="Screenshot of dataset - lastword still has missing values"></a></p>