r - remove last but one character from every field

2019-09-03 15:49发布

问题:

the substr() function in R can isolate any character by position e.g.

substr(df$10,2,3)

or by using nchar() it is possible to work backwards from the end of the field to isolate a character in a position such as last but one using:

substr(df$10,nchar(df$10)-2,nchar(df$10)-1)

however I would like to know how to simply remove the last but one character of every field for a column in a dataframe. I am having difficulty doing this. any help would be great!

回答1:

You can use a regular expression with sub:

x <- c("banana","republic")
sub(".(.)$","\\1",x)
[1] "banaa"   "republc"

What this does is match the last two characters in a string, and replace them with only the last one, which is captured in a capture group.