Please R-gurus, how can I keep the last 9 digits of an alphanumeric string for e.g.
LA XAN 000262999444
RA XAN 000263000507
WA XAN 000263268038
SA XAN 000263000464
000263000463
000263000476
I only want to get
262999444
263000507
263268038
263000464
263000463
263000476
Thanks a lot
It's pretty easy in stringr
because sub_str
interprets negative indices as offsets from the end of the string.
library(stringr)
str_sub(xx, -9, -1)
If you just want the last 9 positions, you could just use substr
:
substr(xx,nchar(xx) - 8,nchar(xx))
assuming that your character vector is stored in xx
. Also, as Hadley notes below, nchar
will return unexpected things if xx
is a factor, not a character vector. His solution using stringr
is definitely preferable.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With