Was it helpful?

Question

How to count the number of words in a string in R?

R ProgrammingServer Side ProgrammingProgramming

The number of words in a sentence could be used for text analysis, therefore, we are required to count them. This can be for a single sentence or for multiple sentences. We can find the number of words in a sentence or in multiple sentences using strsplit with sapply.

Example

Consider the below sentences read as vectors −

> x1<-c("Data Science is actually the Statistical analysis")
> x1
[1] "Data Science is actually the Statistical analysis"
> sapply(strsplit(x1, " "), length)
[1] 7
> x2<-c("China faced trouble even after controlling COVID-19")
> x2
[1] "China faced trouble even after controlling COVID-19"
> sapply(strsplit(x2, " "), length)
[1] 7
> x3<-c("Corona virus has changed everything in the world")
> x3
[1] "Corona virus has changed everything in the world"
> sapply(strsplit(x3, " "), length)
[1] 8
> x4<-c("Corruption is the real threat to the success of any country")
> x4
[1] "Corruption is the real threat to the success of any country"
> sapply(strsplit(x4, " "), length)
[1] 11
> x5<-c("Only unity of people can make lands prosper")
> x5
[1] "Only unity of people can make lands prosper"
> sapply(strsplit(x5, " "), length)
[1] 8
> x6<-c("Small strings are easy to read", "Nobody likes large texts because it's boring",
+ "But the knowledge comes from reading")
> x6
[1] "Small strings are easy to read"
[2] "Nobody likes large texts because it's boring"
[3] "But the knowledge comes from reading"
> sapply(strsplit(x6, " "), length)
[1] 6 7 6
> x7<-c("Quick Math questions are very simple to answer if you understand basic math calculations like division, percentage, ratio, etc.",
+ "It is a known fact that answering puzzles is not so easy but if you practice them then you will be able to build a base for solving puzzles.",
+ "Guesstimation Questions can be answered if you understand the right proxy about the context of the question.",
+ "Data extraction is the first step of programming in Data Science projects and SQL is highly required for this thing.",
+ "R programming and Python are widely used in Data Science. Both of these tools serve the same purpose that is analyzing large data sets.",
+ "Statistics is the base for Data Science and you must have a very good understanding of Statistics concepts to become a Data Scientist.
+ ",
+ "Machine Learning is a major part of Data Science projects. There are many machine learning algorithms that solve complex real-life problems in an easy way if applied correctly.
+ ",
+ "The main purpose of asking a tricky question is to check your critical thinking ability.",
+ "With the help probability, you can calculate whether you should do something or not.
+ ")
> x7
[1] "Quick Math questions are very simple to answer if you understand basic math calculations like division, percentage, ratio, etc."
[2] "It is a known fact that answering puzzles is not so easy but if you practice them then you will
be able to build a base for solving puzzles."
[3] "Guesstimation Questions can be answered if you understand the right proxy about the context of the question."
[4] "Data extraction is the first step of programming in Data Science projects and SQL is highly required for this thing."
[5] "R programming and Python are widely used in Data Science. Both of these tools serve the same purpose that is analyzing large data sets."
[6] "Statistics is the base for Data Science and you must have a very good understanding of Statistics concepts to become a Data Scientist.\n"
[7] "Machine Learning is a major part of Data Science projects. There are many machine learning algorithms that solve complex real-life problems in an easy way if applied correctly.\n"
[8] "The main purpose of asking a tricky question is to check your critical thinking ability."
[9] "With the help probability, you can calculate whether you should do something or not.\n" > sapply(strsplit(x7, " "), length)
[1] 19 29 17 20 24 23 28 15 14
raja
Published on 11-Aug-2020 09:56:35
Advertisements
Was it helpful?
Not affiliated with Tutorialspoint
scroll top