How to find the number of common words between two string vectors in R?


To find the number of common words between two string vectors, we would first need to split both the vectors using unlist and strsplit function and then we can apply length function along with intersect function.

Check out the below examples to understand how it can be done.

Example 1

Following snippet creates a vector −

x1<-"Deep Learning is a subfield of machine learning concerned with algorithms inspired by the structure and function of the brain called artificial neural networks."
y1<-"Deep learning is an artificial intelligence (AI) function that imitates the workings of the human brain in processing data and creating patterns for use in decision making. Deep learning is a subset of machine learning in artificial intelligence that has networks capable of learning unsupervised from data that is unstructured or unlabeled. Also known as deep neural learning or deep neural network."
x1_split <- unlist(strsplit(x1,split=" "))
x1_split

The following vector is created −

[1] "Deep" "Learning" "is" "a" "subfield"
[6] "of" "machine" "learning" "concerned" "with"
[11] "algorithms" "inspired" "by" "the" "structure"
[16] "and" "function" "of" "the" "brain"
[21] "called" "artificial" "neural" "networks."

To find the number of common words between two string vectors, add the following code to the above snippet −

x1<-"Deep Learning is a subfield of machine learning concerned with algorithms inspired by the structure and function of the brain called artificial neural networks."
y1<-"Deep learning is an artificial intelligence (AI) function that imitates the workings of the human brain in processing data and creating patterns for use in decision making. Deep learning is a subset of machine learning in artificial intelligence that has networks capable of learning unsupervised from data that is unstructured or unlabeled. Also known as deep neural learning or deep neural network."
y1_split <- unlist(strsplit(y1,split=" "))
y1_split

Output

If you execute all the above given snippets as a single program, it generates the following output −

[1] "Deep" "learning" "is" "an" "artificial"
[6] "intelligence" "(AI)" "function" "that" "imitates"
[11] "the" "workings" "of" "the" "human"
[16] "brain" "in" "processing" "data" "and"
[21] "creating" "patterns" "for" "use" "in"
[26] "decision" "making." "Deep" "learning" "is"
[31] "a" "subset" "of" "machine" "learning"
[36] "in" "artificial" "intelligence" "that" "has"
[41] "networks" "capable" "of" "learning" "unsupervised"
[46] "from" "data" "that" "is" "unstructured"
[51] "or" "unlabeled." "Also" "known" "as"
[56] "deep" "neural" "learning" "or" "deep"
[61] "neural" "network."

To find the number of common words between two string vectors, add the following code to the above snippet −

length(intersect(x1_split,y1_split))

Output

If you execute all the above given snippets as a single program, it generates the following output −

[1] 12

Example 2

Following snippet creates a vector −

x2<-"Digital marketing is the act of promoting and selling products and services by leveraging online marketing tactics such as social media marketing, search marketing, and email marketing."
y2<-"Basically, digital marketing refers to any online marketing efforts or assets. Email marketing, pay-per-click advertising, social media marketing and even blogging are all great examples of digital marketing—they help introduce people to your company and convince them to buy."
x2_split<-unlist(strsplit(x2,split=" "))
x2_split

The following vector is created −

[1] "Digital" "marketing" "is" "the" "act"
[6] "of" "promoting" "and" "selling" "products"
[11] "and" "services" "by" "leveraging" "online"
[16] "marketing" "tactics" "such" "as" "social"
[21] "media" "marketing," "search" "marketing," "and"
[26] "email" "marketing."

To find the number of common words between two string vectors, add the following code to the above snippet −

x2<-"Digital marketing is the act of promoting and selling products and services by leveraging online marketing tactics such as social media marketing, search marketing, and email marketing."
y2<-"Basically, digital marketing refers to any online marketing efforts or assets. Email marketing, pay-per-click advertising, social media marketing and even blogging are all great examples of digital marketing—they help introduce people to your company and convince them to buy."
y2_split<-unlist(strsplit(y2,split=" "))
y2_split

Output

If you execute all the above given snippets as a single program, it generates the following output −

[1] "Basically," "digital" "marketing" "refers"
[5] "to" "any" "online" "marketing"
[9] "efforts" "or" "assets." "Email"
[13] "marketing," "pay-per-click" "advertising," "social"
[17] "media" "marketing" "and" "even"
[21] "blogging" "are" "all" "great"
[25] "examples" "of" "digital" "marketing—they"
[29] "help" "introduce" "people" "to"
[33] "your" "company" "and" "convince"
[37] "them" "to" "buy."

To find the number of common words between two string vectors, add the following code to the above snippet −

length(intersect(x2_split,y2_split))

Output

If you execute all the above given snippets as a single program, it generates the following output −

[1] 7

Example 3

Following snippet creates a vector −

x3<-"Data science is an essential part of any industry today, given the massive amounts of data that are produced. Data science is one of the most debated topics in the industries these days. Its popularity has grown over the years, and companies have started implementing data science techniques to grow their business and increase customer satisfaction. In this article, we’ll learn what data science is, and how you can become a data scientist."
y3<-"As the world entered the era of big data, the need for its storage also grew. It was the main challenge and concern for the enterprise industries until 2010. The main focus was on building a framework and solutions to store data. Now when Hadoop and other frameworks have successfully solved the problem of storage, the focus has shifted to the processing of this data. Data Science is the secret sauce here. All the ideas which you see in Hollywood sci-fi movies can actually turn into reality by Data Science. Data Science is the future of Artificial Intelligence. Therefore, it is very important to understand what is Data Science and how can it add value to your business."
x3_split<-unlist(strsplit(x3,split=" "))
x3_split

The following vector is created −

[1] "Data" "science" "is" "an"
[5] "essential" "part" "of" "any"
[9] "industry" "today," "given" "the"
[13] "massive" "amounts" "of" "data"
[17] "that" "are" "produced." "Data"
[21] "science" "is" "one" "of"
[25] "the" "most" "debated" "topics"
[29] "in" "the" "industries" "these"
[33] "days." "Its" "popularity" "has"
[37] "grown" "over" "the" "years,"
[41] "and" "companies" "have" "started"
[45] "implementing" "data" "science" "techniques"
[49] "to" "grow" "their" "business"
[53] "and" "increase" "customer" "satisfaction."
[57] "In" "this" "article," "we’ll"
[61] "learn" "what" "data" "science"
[65] "is," "and" "how" "you"
[69] "can" "become" "a" "data"
[73] "scientist."

To find the number of common words between two string vectors, add the following code to the above snippet

x3<-"Data science is an essential part of any industry today, given the massive amounts of data that are produced. Data science is one of the most debated topics in the industries these days. Its popularity has grown over the years, and companies have started implementing data science techniques to grow their business and increase customer satisfaction. In this article, we’ll learn what data science is, and how you can become a data scientist."
y3<-"As the world entered the era of big data, the need for its storage also grew. It was the main challenge and concern for the enterprise industries until 2010. The main focus was on building a framework and solutions to store data. Now when Hadoop and other frameworks have successfully solved the problem of storage, the focus has shifted to the processing of this data. Data Science is the secret sauce here. All the ideas which you see in Hollywood sci-fi movies can actually turn into reality by Data Science. Data Science is the future of Artificial Intelligence. Therefore, it is very important to understand what is Data Science and how can it add value to your business."
y3_split<-unlist(strsplit(y3,split=" "))
y3_split

Output

If you execute all the above given snippets as a single program, it generates the following output −

[1] "As" "the" "world" "entered"
[5] "the" "era" "of" "big"
[9] "data," "the" "need" "for"
[13] "its" "storage" "also" "grew."
[17] "It" "was" "the" "main"
[21] "challenge" "and" "concern" "for"
[25] "the" "enterprise" "industries" "until"
[29] "2010." "The" "main" "focus"
[33] "was" "on" "building" "a"
[37] "framework" "and" "solutions" "to"
[41] "store" "data." "Now" "when"
[45] "Hadoop" "and" "other" "frameworks"
[49] "have" "successfully" "solved" "the"
[53] "problem" "of" "storage," "the"
[57] "focus" "has" "shifted" "to"
[61] "the" "processing" "of" "this"
[65] "data." "Data" "Science" "is"
[69] "the" "secret" "sauce" "here."
[73] "All" "the" "ideas" "which"
[77] "you" "see" "in" "Hollywood"
[81] "sci-fi" "movies" "can" "actually"
[85] "turn" "into" "reality" "by"
[89] "Data" "Science." "Data" "Science"
[93] "is" "the" "future" "of"
[97] "Artificial" "Intelligence." "Therefore," "it"
[101] "is" "very" "important" "to"
[105] "understand" "what" "is" "Data"
[109] "Science" "and" "how" "can"
[113] "it" "add" "value" "to"
[117] "your" "business."

To find the number of common words between two string vectors, add the following code to the above snippet −

length(intersect(x3_split,y3_split))

Output

If you execute all the above given snippets as a single program, it generates the following output −

[1] 16

Updated on: 11-Nov-2021

422 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements