Extract last two words of a string problem

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?

Try this:

stringr::str_extract(strings, "(\\w*\\s)?\\w*$") 
[1] "knobbed hornbill" "Togo toucan"      "wood duck"        "grey squirrel"    "Aardvark"
2 Likes

@williaml 's answer is more elegant, but here's another option:

  library(tidyverse)
  coalesce(word(strings, -2, -1), word(strings, -1, -1))

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.

2 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.