• Post author:
  • Post category:R Programming
  • Post last modified:June 27, 2024
  • Reading time:9 mins read

How to compare two strings in R? You can use the == operator to compare strings in both a case-sensitive and case-insensitive manner. String comparison is a fundamental aspect of data manipulation and analysis in R. In this article, we will explore various methods to compare strings in R using different operators and functions, including those in base R and packages like stringr.

Key Points-

  • The == operator checks if two strings are exactly equal, considering case sensitivity.
  • By converting strings to lowercase with tolower(), the == operator can perform case-insensitive comparisons.
  • The identical() function checks if two objects (strings) are exactly the same, often used with tolower() for case-insensitive comparisons.
  • The grepl() function searches for a pattern within a string and returns TRUE if the pattern is found.
  • The startsWith() function checks if a string begins with a specified prefix and returns TRUE if it does.
  • The endsWith() function checks if a string ends with a specified suffix and returns TRUE if it does.
  • The str_detect() function from the stringr package searches for a pattern within a string and returns TRUE if the pattern is found.
  • The nchar() function returns the number of characters in a string, allowing length comparisons using the == operator.
  • Comparing strings is fundamental in data manipulation and analysis, useful for filtering and matching data.

Compare Two Strings Using == Operator in Case-Sensitive

You can use the == operator to check if two strings are equal. When used case-sensitively, this operator compares the exact characters in both strings. If the strings are identical, it returns TRUE; otherwise, it returns FALSE.


# Compare two strings using == operator in case sensitve
str1 <- "SPARKBYEXAMPLES"
str2 <- "sparkbyexamples"
print("Given strings")
str1 
str2 
result <- str1 == str2
print("After comparing two strings in case-sensitive manner")
print(result)    

Yields below output.

Compare R Stings

Compare Two Strings Using == in a Case-insensitive Manner

Alternatively, you can use the == operator in a case-insensitive manner. By converting both strings to lowercase using the tolower() function, you can then compare the lowercase versions with the == operator. If the strings are equal, it will return TRUE; otherwise, it will return FALSE.


# Compare two strings using == operator
str1 <- "SPARKBYEXAMPLES"
str2 <- "sparkbyexamples"
print("Given strings")
str1
str2
result <- tolower(str1) == tolower(str2)

print("After comparing two strings in case-insensitive manner")
print(result) 

The above code has returned the result of the comparison is TRUE because "SparkByExamples" and "sparkbyexamples" are identical when converted to lowercase.

Yields below output.

Compare R Stings

Compare Two Strings using identical() Function

To compare two strings, you can use the identical() function, which checks if two objects are the same. When comparing two strings in a case-insensitive manner, you can convert both strings to either lowercase using the tolower() function or uppercase using the toupper() function. After converting them to the same case, use the identical() function to check if they are equal. If the strings are identical, it will return TRUE; otherwise, it will return FALSE.


# Compare two strings using identical() function
str1 <- "SPARKBYEXAMPLES"
str2 <- "sparkbyexamples"
print("Given strings")
print(str1)
print(str2)

result <- identical(tolower(str1),tolower(str2)) 
print("After comparing two strings:")
print(result)

# Output:
# [1] "Given strings"
# [1] "SPARKBYEXAMPLES"
# [1] "sparkbyexamples"
# [1] "After comparing two strings:"
# [1] TRUE

Using grepl() Compare Strings in R

You can use the grepl() function to perform partial string comparisons. It searches for a specified pattern within a string and returns TRUE if the pattern is found, and FALSE otherwise. Let’s define a string and a substring (pattern), and use this function to check if the substring exists within the string.


# Compare two strings using grepl()
str <- "SparkByExamples"
pattern <- "Examples"
result <- grepl(pattern, str)
print("After comparing two strings")
print(result)

# Output:
# [1] "After comparing two strings"
# [1] TRUE

Using startsWith() to Compare the Strings

You can use the startsWith() function to compare strings in R. First, define the string and the substring you want to check as a prefix. Then, use the startsWith() function to determine if the string starts with the specified prefix. The function returns TRUE if the string begins with the specified prefix.


# Compare two strings using statrsWith()
str <- "SparkByExamples"
prefix <- "Spark"
result <- startsWith(str, prefix)
print("After comparing two strings")
print(result) 

# Output:
# [1] "After comparing two strings"
# [1] TRUE

Using endsWith() to Compare the Strings in R

In this example, you can use the endsWith() function to compare strings in R. First, define a string and the specified suffix, which is a substring of the string. Then, use the endsWith() function to check if the string ends with the specified suffix. The function returns TRUE if the string ends with the given suffix.


# Compare two strings using endswith() function
str <- "SparkByExamples"
suffix <- "Examples"
result <- endssWith(str, suffix)
print("After comparing two strings")
print(result)

# Output:
# [1] "After comparing two strings"
# [1] TRUE

Using str_detect() from stringr package

The str_detect() function from the stringr package is used to compare strings in R. It searches for a pattern within a string and returns TRUE if the pattern is found.


# Compare two strings using stringr package
library(stringr)
str <- "SparkByExamples"
pattern <- "Examples"
result <- str_detect(str, pattern)
print("After comparing two strings")
print(result)

# Output:
# [1] "After comparing two strings"
# [1] TRUE

Using nchar() to Compare Strings Lengths

Finally, you can use the nchar() function to compare the lengths of two strings. By comparing the lengths of the given strings using the == operator, it will check if the lengths are equal, returning TRUE if they are, and FALSE otherwise.

In this

# Compare two strings using nchar() and == operator
str1 <- "SparkByExamples"
str2 <- "Examples"

result <- nchar(str1) == nchar(str2)
print("After comparing two strings")
print(result) 

# Output:
# [1] "After comparing two strings"
# [1] FALSE

Conclusion

In this article, I have explained multiple approaches in R for comparing two strings, including exact matches, pattern detection, case-insensitive comparisons, and checking prefixes and suffixes. Also explained the comparison of strings based on their length.