Extract last word in string in R

前端 未结 5 1716
情深已故
情深已故 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条回答
  •  臣服心动
    2020-11-29 06:19

    Going in the package direction, this is the simplest answer I can think of:

    library(stringr)
    
    x <- 'The quick brown fox'
    str_extract(x, '\\w+$')
    #[1] "fox"
    

提交回复
热议问题