by @codeexamples

Getting the last n characters from a string in R

Home » r » Getting the last n characters from a string in R

To get the last n characters from a string, we can use the stri_sub() function from a stringi package in R.

Here is an example:

stri_sub("cabin",1,3)
[1] "cab"
stri_sub("cabin",1,1)
[1] "c"
stri_sub("cabin",-3,-1)
[1] "bin"

You can install it on CRAN by using:

install.packages("stringi")