Learn R Programming

DescTools (version 0.99.52)

StrCountW: Count Words in a String

Description

Count the number of words that appear within a character string.

Usage

StrCountW(x)

Value

an integer defining the number of word in the string

Arguments

x

a vector of strings to be parsed.

Author

Andri Signorell <andri@signorell.net>, based on code from Adam Bradley <hisself@adambradley.net>

Details

This is just a wrapper for a fine regexpr. It uses the expression \b\W+\b to separate the words. The code \W is equivalent to [^[:alnum:]_]) wheras [:alnum:] contains [:alpha:] and [:digit:]. So everything that is not an alphanumeric character, a digit or a _ (underscore) is used as separator for the words to be counted.

References

http://stackoverflow.com/questions/8920145/count-the-number-of-words-in-a-string-in-r

See Also

Examples

Run this code
StrCountW("This is a true story!")

StrCountW("Just_one_word")
StrCountW("Not-just.one/word")

StrCountW("And what about numbers 8899 or special characters $$$/*?")
StrCountW("   Starting'n ending with some whitespace  ")

StrCountW(c("This is a", "text in more", "than one line."))

Run the code above in your browser using DataLab