Textual Data, String Handling, Regular Expressions, and Data Structures
摘要
This chapter includes procedures for representing and manipulating text in R for marketing analytics. It begins with the rationale for text cleaning and provides examples of the difficulties presented by user-generated text—misspellings, alternative spellings, abbreviations, emojis, and mixed formats—and how these affect some practical examples. The chapter then introduces the primitive R data structures, how text is stored and accessed, and how to vectorize it. The practice sections provide examples for creating and indexing character vectors; concatenating character strings; measuring and summarizing string lengths; and exploring a product reviews data set with simple exploratory graphics. The gentle but thorough introduction to regular expressions covers many of the basic elements to consider when working with them. Practical issues and tips, including spell-checking and harmonizing language variants, will also be explored, including the pitfalls of automatic correction. Along the way, the short, reproducible code snippets and exercises will help convert new concepts into applied skills to create a robust preprocessing pipeline.