如何在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 次瀏覽

啟動您的職業生涯

完成課程獲得認證

開始學習
廣告