In R code I have a character variable var that has values \"AA\", \"AB\", \"AC\", etc.
var
str(var) chr [1:17003] \"AA\" \"AA\" \"AA\" \"AA\" \"AB\"
You can convert the string to a factor and then to numeric.
x <- c("AA", "AB", "AB", "AC", "AA", "XY") as.numeric(as.factor(x)) # [1] 1 2 2 3 1 4
Alternatively, you can use match and unique:
match
unique
match(x, unique(x)) # [1] 1 2 2 3 1 4