rstringword-count

Count the number of all words in a string


Is there a function to count the number of words in a string? For example:

str1 <- "How many words are in this sentence"

to return a result of 7.


Solution

  • You can use strsplit and sapply functions

    sapply(strsplit(str1, " "), length)