To get the last N characters of a given string in R, you can use the `substr` function with a negative start parameter.
In this example,
str1
with a string value of "Hello World"
.substr
function with substr(str1, start = -5, stop = nchar(str1))
to get the last 5 characters.str1 <- "Hello World"
last5Chars <- substr(str1, start = -5, stop = nchar(str1))
print(paste("Last 5 characters of the string are:", last5Chars))
Last 5 characters of the string are: World
In this example,
str2
with a string value of "apple"
.substr
function with substr(str2, start = -3, stop = nchar(str2))
to get the last 3 characters.str2 <- "apple"
last3Chars <- substr(str2, start = -3, stop = nchar(str2))
print(paste("Last 3 characters of the string are:", last3Chars))
Last 3 characters of the string are: ple
In this tutorial, we learned How to get Last N Characters in String in R language with well detailed examples.