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

Removing unneeded character spaces

We can start by removing leading and trailing blanks from each of the product descriptions, since they add no value to the analysis and just take up extra space. trimws is a handy function to accomplish this, since it removes both leading and trailing spaces. The nchar() function will count the number of bytes in a character string, so we can run this function on OnlineRetail$Description before and after performing the string trim to see how much space we will actually be saving:

sum(nchar(OnlineRetail$Description))

This is the resulting output:

> [1] 14284888

Continue the following code:

OnlineRetail$Description <- trimws(OnlineRetail$Description)sum(nchar(OnlineRetail$Description)) 

This is the resulting ...

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