How to Count the Number of Words in a Character String in R (Example Code)
In this R programming tutorial you’ll learn how to find the amount of words in a sentence.
Constructing Example Data
my_string <- "xx aaaa ++ hello -§$% yyy whats up -" # Creating example character string |
my_string <- "xx aaaa ++ hello -§$% yyy whats up -" # Creating example character string
Example: Identify Number of Real Words in Character String
lengths(gregexpr("\W+", my_string)) + 1 # Applying gregexpr & lengths functions # 7 |
lengths(gregexpr("\W+", my_string)) + 1 # Applying gregexpr & lengths functions # 7