1

So I would like my code below to return TRUE, even as the front 2 letters are different.

Is there a way to accomplish this? I know == does not work as it compares both exactly.

if("UKVICTORIA" == "USVICTORIA") {
  print("TRUE")} else {
    print("FALSE")
  }
}
Jilber Urbina
  • 58,147
  • 10
  • 114
  • 138
user1464667
  • 133
  • 1
  • 2
  • 7

2 Answers2

2

Use agrepl

> agrepl("UKVICTORIA", "USVICTORIA", max.distance = 1)
[1] TRUE

Note, if there is an extra character (Z), it returns FALSE

> agrepl("UZKVICTORIA", "USVICTORIA", max.distance = 1)
[1] FALSE
Jilber Urbina
  • 58,147
  • 10
  • 114
  • 138
0

Remove first two characters and check the number of unique values.

length(unique(sub(".{2}", "", c("UKVICTORIA", "USVICTORIA")))) == 1
#[1] TRUE
d.b
  • 32,245
  • 6
  • 36
  • 77