0 votes
1 view
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 (25.4k points)
edited by
 
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

Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...