Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in R Programming by (5.3k points)

How can I get the last n characters from a string in R? Is there a function like SQL's RIGHT?

closed

1 Answer

0 votes
by
edited
 
Best answer

To extract last n characters from a string, you can use the str_sub function from the stringi package as follows:

stri_sub(x,i,n)

Where

- String/Vector

- Start index

- Final index

For example:

library("stringi") 

vec <- c("Hello World") 

str <- stri_sub(vec,-5,-1) 

str 

Output: 

[1] "World"

If you want to explore more in R programming then watch this R programming tutorial for beginner:

Related questions

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...