Back

Explore Courses Blog Tutorials Interview Questions
0 votes
1 view
in R Programming by (5.3k points)

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.

1 Answer

0 votes
by

You can use the sapply and strsplit functions as follows:

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

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

[1] 7

Welcome to Intellipaat Community. Get your technical queries answered by top developers!

28.4k questions

29.7k answers

500 comments

94.7k users

Browse Categories

...