I'm trying to get the last few words of some strings, but running into issues with one-word strings. Ideally, I'd like the last two words of animal names, but only one word if there is only one word.
The problem is with strings that have only one word. I want it to return one word, such as "Aardvark", but instead this one word string returns NA. Is there a way to do this?
This combines your current answer for the 2nd-to-last word plus a backup option when that is NA, where it uses the last word. dplyr::coalesce returns the first non-NA value.