Back
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.
You can use the sapply and strsplit functions as follows:
str1 <- "How many words are in this sentence"sapply(strsplit(str1, " "), length)[1] 7
sapply(strsplit(str1, " "), length)
[1] 7
31k questions
32.8k answers
501 comments
693 users