如何计算 R 中字符串中的单词数量?


句子中的单词数量可用于文本分析,因此我们需要对单词数量进行计数。这可以针对单个句子或多个句子进行。我们可以使用带 sapply 的 strsplit 查找句子或多个句子中的单词数量。

示例

考虑如下作为向量读取的句子 −

> 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.
" [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.
" [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.
" > sapply(strsplit(x7, " "), length) [1] 19 29 17 20 24 23 28 15 14

更新于: 2020 年 8 月 11 日

482 次浏览

开启您的 职业生涯

通过完成该课程来获得认证

开始
广告