Extract last word in string in R

前端 未结 5 1718
情深已故
情深已故 2020-11-29 05:53

What\'s the most elegant way to extract the last word in a sentence string?

The sentence does not end with a \".\" Words are seperated by blanks.

sen         


        
5条回答
  •  猫巷女王i
    2020-11-29 06:26

    Another packaged option is stri_extract_last_words() from the stringi package

    library(stringi)
    
    stri_extract_last_words("The quick brown fox")
    # [1] "fox"
    

    The function also removes any punctuation that may be at the end of the sentence.

    stri_extract_last_words("The quick brown fox? ...")
    # [1] "fox"
    

提交回复
热议问题