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

Example: Identify Number of Real Words in Character String

lengths(gregexpr("\W+", my_string)) + 1             # Applying gregexpr & lengths functions
# 7

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed