How to Compare Strings in R with examples

[This article was first published on Data Analysis in R » Quick Guide for Statistics & R » finnstats, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

The post How to Compare Strings in R with examples appeared first on finnstats.

If you want to read the original article, click here How to Compare Strings in R with examples.

In R, you can compare strings using the following methods.

R Plot pch Symbols: Different point shapes in R » finnstats

Comparing Two Strings

Approach 1:-

Suppose you are looking for a case-sensitive comparison

string1 == string2

In another case -insensitive comparison

tolower(string1) == tolower(string2)

Approach 2:

Compare Two Vectors of Strings

Assume a case-sensitive comparison

identical(vector1, vector2)

In another case-insensitive comparison

Aesthetics must be either length 1 or the same as the data » finnstats

identical(tolower(vector1), tolower(vector2))

Approach 3:

Find Similarities Between Two Vectors of Strings

Let’s see if any of the strings in vector1 are also present in vector2.

vector1[vector1 %in% vector2] 

The examples below demonstrate how to apply each strategy in practice.

Example 1: Check if two vectors are identical

The code below demonstrates how to compare two strings in R to see if they are equal.

How to Read rda file in R (with Example) » finnstats

Now we can define two strings

string1 <- "Hello"
string2 <- "hello"

In the case-sensitive comparison

string1 == string2
[1] FALSE

Suppose case-insensitive comparison

tolower(string1) == tolower(string2)
[1] TRUE

Because the two strings aren’t completely similar, the case-sensitive comparison returns FALSE.

However, because the two strings contain the same characters in the same order, regardless of the case, the case-insensitive comparison returns TRUE.

McNemar’s test in R » finnstats

Example 2: Compare Two Vectors of Strings

The following code demonstrates how to use the identical() function to compare two string vectors.

Let’s define two vectors of strings

vector1 <- c("This", "is", "egg")
vector2 <- c("This", "is", "Egg")

Now we can do two vectors case-sensitive comparison

identical(vector1, vector2)
[1] FALSE

Ok, let’s the try case-insensitive comparison

identical(tolower(vector1), tolower(vector2))
[1] TRUE

Because the two vectors do not include the exact same strings in the same case, the case-sensitive comparison returns FALSE.

The case-insensitive comparison, on the other hand, returns TRUE since both vectors contain the same strings, regardless of case.

Regression analysis in R-Model Comparison » finnstats

Example 3: Find String Similarities Between Two Vectors

The following code demonstrates how to discover which strings in one vector belong to another vector using the percent in percent operator:

Create two string vectors.

vector1 <- c("Hi", "sony", "how are you")
vector2 <- c("hi", "sony", "how are you")

Let’s see if any of the strings in vector1 are also present in vector2.

vector1[vector1 %in% vector2]
[1] "sony"        "how are you"

The strings “hey” and “hello” appear in both vector1 and vector2 according to the result.

One of the First Steps to Become a Data Scientist » finnstats

To read more visit How to Compare Strings in R with examples.

If you are interested to learn more about data science, you can find more articles here finnstats.

The post How to Compare Strings in R with examples appeared first on finnstats.

To leave a comment for the author, please follow the link and comment on their blog: Data Analysis in R » Quick Guide for Statistics & R » finnstats.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)