Perhaps you should look at substr
instead:
substr(x, nchar(x), nchar(x))
# [1] "2" "2" "1"
This is essentially, "Start at the last character and end at the last character".
or...
substring(x, nchar(x))
# [1] "2" "2" "1"
There's also a great package called "stringi" that has a lot of convenient string functions. For this problem, you can use stri_sub
:
library(stringi)
stri_sub(x, -1)
# [1] "2" "2" "1"