To remove the last N characters of a string in R, you can use the substr function with the range from 1 to nchar(string) - N.
In this example,
str1
with a value of 'Hello World'
.str1 <- 'Hello World'
modifiedStr <- substr(str1, 1, nchar(str1) - 3)
cat('Modified string is: ', modifiedStr)
Modified string is: Hello Wo
In this example,
str2
with a value of 'apple'
.str2 <- 'apple'
modifiedStr <- substr(str2, 1, nchar(str2) - 2)
cat('Modified string is: ', modifiedStr)
Modified string is: app
In this tutorial, we learned How to Remove last N Characters in a String in R language with well detailed examples.