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.
Here's a reprex:
strings <- c("Red-knobbed hornbill", "Togo toucan", "white-winged wood duck", "eastern grey squirrel", "Aardvark")
word(strings, -2, -1))
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?