OneBite.Dev - Coding blog in a bite size

compare two strings in R

Code snippet on how to compare two strings in R

  str1 <- "cat"
  str2 <- "dog"
 
  x <- identical(str1, str2)
  print(x)

This code runs a comparison between two strings. The first two lines create two strings stored in the variables str1 and str2. Then the third line runs the command “identical,” which tests if the two strings have the same value. It returns a boolean (true or false) value that is stored in x. The final line prints out x. If the strings are the same, it will print out “TRUE.” Otherwise, it will print out “FALSE.”

r