Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Select a substring of characters from the end of a character string in R

I have a vector of character strings

vec <- c("1ZQOYNBAA55", "2JSNHGKLRBB66", "3HVXCC77", "4LDD88", "5CIFMTLYXEE99")

> vec
[1] "1ZQOYNBAA55"   "2JSNHGKLRBB66" "3HVXCC77"      "4LDD88"        "5CIFMTLYXEE99"

…and I would like to get the last 3 characters from each string. To get the first 3 characters, I can use substr()

substr(vec,1,3)

I would have thought something like substr() with a "fromLast" argument might exist

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

vec_ends <- substr(vec,1,3, fromLast = TRUE)

With an expected output

> vec_ends
[1] "A55" "B66" "C77" "D88" "E99"

But substr() only works one way. In my dataset the string lengths are variable so no reference to absolute character numbers or string lengths can be made, and there are no consistent separators of delimiting characters for a string split. Does anyone know of an easy way to do this in R?

>Solution :

You could use a sub() approach:

vec_ends <- sub(".*(.{3})$", "\\1", vec)
vec_ends

[1] "A55" "B66" "C77" "D88" "E99"
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading