O'Reilly logo

Practical Predictive Analytics by Ralph Winters

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Extracting the last word

Often the first and last word of product descriptions contain useful information, and sometimes you can use a single word or phrase in place of the original longer description. This may not always be the case, but it is worth trying. In order to extract the last word from the descriptions, we can use the word function from the stringr package:

library(stringr)OnlineRetail$lastword <- word(OnlineRetail$Description, -1)  #supply -1 to extract the last wordOnlineRetail$Description <- trimws(OnlineRetail$Description, "l")OnlineRetail$firstword <- word(OnlineRetail$Description, 1)# use head(OnlineRetail) if you are no using Rmarkdownkable(OnlineRetail[1:5, c(3, 10:12)], padding = 0) 

Firstword appears as the last column ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required