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

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

查看以下示例以了解如何完成。

示例 1

>x1=c(“数据科学是一个跨学科领域,它使用科学方法、流程、算法和系统从结构化和非结构化数据中提取知识和见解,并将数据中的知识和可操作的见解应用于广泛的应用领域。” ,“数据科学是一个研究领域,它使用现代工具和技术处理大量数据,以发现看不见的模式、获取有意义的信息并做出业务决策。数据科学使用复杂的机器学习算法来构建预测模型。”)

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
输出结果

如果您执行上面给定的代码段,它会生成以下输出 -

[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
输出结果

如果您执行上面给定的代码段,它会生成以下输出 -

[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"

猜你喜欢