How Do You Compare Two Strings in R?

It’s pretty simple actually.

s1 <- "string"
s2 <- "String"

# Case-sensitive check
s1 == s2

# Case-insensitive check
tolower(s1) == tolower(s2)

The output in the first case is FALSE and in the second case it is TRUE. You can use toupper() as well.

Leave a Comment

tech