如何在R语言中查找字符串向量中元素的交集?


如果我们有一个包含多个元素的字符串向量,那么所有元素中可能存在一些共同的值。如果我们想找到这些值,可以使用 intersect 函数以及 strsplit 函数和 Reduce 函数。

查看下面的示例以了解如何操作。

示例 1

>x1=c("Data science is an interdisciplinary field that uses scientific methods, processes, algorithms and systems to extract knowledge and insights from structured and unstructured data, and apply knowledge and actionable insights from data across a broad range of application domains.","Data science is the domain of study that deals with vast volumes of data using modern tools and techniques to find unseen patterns, derive meaningful information, and make business decisions. Data science uses complex machine learning algorithms to build predictive models.")

x1

输出

如果您执行上述代码片段,它将生成以下输出:

[1] "Data science is an interdisciplinary field that uses scientific methods,
processes, algorithms and systems to extract knowledge and insights from
structured and unstructured data, and apply knowledge and actionable insights
from data across a broad range of application domains."
[2] "Data science is the domain of study that deals with vast volumes of data
using modern tools and techniques to find unseen patterns, derive meaningful
information, and make business decisions. Data science uses complex machine
learning algorithms to build predictive models."

将以下代码添加到上述代码片段中:

x1
Reduce(intersect, strsplit(x1," "))

输出

如果您将上述所有代码片段作为一个程序执行,它将生成以下输出:

[1] "Data" "science" "is" "that" "uses"
[6] "algorithms" "and" "to" "data" "of"

示例 2

>x2<-c("Machine learning is a method of data analysis that automates analytical model building. It is a branch of artificial intelligence based on the idea that systems can learn from data, identify patterns and make decisions with minimal human intervention.","Machine learning is an application of artificial intelligence (AI) that provides systems the ability to automatically learn and improve from experience without being explicitly programmed. Machine learning focuses on the development of computer programs that can access data and use it to learn for themselves.","Machine Learning is the field of study that gives computers the capability to learn without being explicitly programmed. ML is one of the most exciting technologies that one would have ever come across. As it is evident from the name, it gives the computer that makes it more similar to humans: The ability to learn. Machine learning is actively being used today, perhaps in many more places than one would expect.")

x2

输出

如果您执行上述代码片段,它将生成以下输出:

[1] "Machine learning is a method of data analysis that automates analytical
model building. It is a branch of artificial intelligence based on the idea
that systems can learn from data, identify patterns and make decisions with
minimal human intervention."
[2] "Machine learning is an application of artificial intelligence (AI) that
provides systems the ability to automatically learn and improve from experience
without being explicitly programmed. Machine learning focuses on the
development of computer programs that can access data and use it to learn for
themselves."
[3] "Machine Learning is the field of study that gives computers the capability
to learn without being explicitly programmed. ML is one of the most exciting
technologies that one would have ever come across. As it is evident from the
name, it gives the computer that makes it more similar to humans: The ability
to learn. Machine learning is actively being used today, perhaps in many more
places than one would expect."

将以下代码添加到上述代码片段中:

x2
Reduce(intersect,strsplit(x2," "))

输出

如果您将上述所有代码片段作为一个程序执行,它将生成以下输出:

[1] "Machine" "learning" "is" "of" "that" "the" "learn"
[8] "from"

示例 3

>x3<-c("Deep Learning is a subfield of machine learning concerned with algorithms inspired by the structure and function of the brain called artificial neural networks.","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.","Deep learning is a machine learning technique that teaches computers to do what comes naturally to humans: learn by Example. Deep learning is a key technology behind driverless cars, enabling them to recognize a stop sign, or to distinguish a pedestrian from a lamppost. It is the key to voice control in consumer devices like phones, tablets, TVs, and handsfree speakers. Deep learning is getting lots of attention lately and for good reason. It’s achieving results that were not possible before.","Deep learning can be considered as a subset of machine learning. It is a field that is based on learning and improving on its own by examining computer algorithms. While machine learning uses simpler concepts, deep learning works with artificial neural networks, which are designed to imitate how humans think and learn. Until recently, neural networks were limited by computing power and thus were limited in complexity. However, advancements in Big Data analytics have permitted larger, sophisticated neural networks, allowing computers to observe, learn, and react to complex situations faster than humans. Deep learning has aided image classification, language translation, speech recognition. It can be used to solve any pattern recognition problem and without human intervention.")

x3

输出

如果您执行上述代码片段,它将生成以下输出:

[1] "Deep Learning is a subfield of machine learning concerned with algorithms
inspired by the structure and function of the brain called artificial neural
networks."
[2] "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."
[3] "Deep learning is a machine learning technique that teaches computers to do
what comes naturally to humans: learn by Example. Deep learning is a key
technology behind driverless cars, enabling them to recognize a stop sign, or
to distinguish a pedestrian from a lamppost. It is the key to voice control in
consumer devices like phones, tablets, TVs, and hands-free speakers. Deep
learning is getting lots of attention lately and for good reason. It’s
achieving results that were not possible before."
[4] "Deep learning can be considered as a subset of machine learning. It is a
field that is based on learning and improving on its own by examining computer
algorithms. While machine learning uses simpler concepts, deep learning works
with artificial neural networks, which are designed to imitate how humans think
and learn. Until recently, neural networks were limited by computing power and
thus were limited in complexity. However, advancements in Big Data analytics
have permitted larger, sophisticated neural networks, allowing computers to
observe, learn, and react to complex situations faster than humans. Deep
learning has aided image classification, language translation, speech
recognition. It can be used to solve any pattern recognition problem and
without human intervention."

将以下代码添加到上述代码片段中:

x3
Reduce(intersect,strsplit(x3," "))

输出

如果您执行上述代码片段,它将生成以下输出:

[1] "Deep" "is" "a" "of" "machine" "learning" "and"

更新于:2021年11月2日

617 次浏览

启动您的职业生涯

完成课程获得认证

开始学习
广告