If you’re open to using packages, plyr
is a very popular one and has this handy mapvalues() function that will do just what you’re looking for:
foo <- mapvalues(foo, from=c("AA", "AC", "AG"), to=c("0101", "0102", "0103"))
Note that it works for data types of all kinds, not just strings.