1

I'd like to remove the last character of a string if there is any letter at the end of it.

sample vector is:

string <- c("L_3_A_L_3_1b",  "L_3_A_L_3_1e",  "L_3_A_L_3_1h", "RL_3_C_RL_3_9", "SL_3_A_SL_3_2")

I'd like to drop the last letter as:

> string
[1] "L_3_A_L_3_1"  "L_3_A_L_3_1"  "L_3_A_L_3_1"  "RL_3_C_RL_3_9" "SL_3_A_SL_3_2"

ANy thoughts? Thanks!

amisos55
  • 1,079
  • 7
  • 13

3 Answers3

2

We can specify the letters ([A-Za-z]) specifically (as the OP's post is if there is any letter at the end of it) to match at the end ($) of the string and replace with blank ("")

sub("[A-Za-z]$", "", string)
#[1] "L_3_A_L_3_1"   "L_3_A_L_3_1"   "L_3_A_L_3_1" 
#[4] "RL_3_C_RL_3_9" "SL_3_A_SL_3_2"

Or use trimws

trimws(string, whitespace = '[A-Za-z]', which = 'right')
#[1] "L_3_A_L_3_1"   "L_3_A_L_3_1"   "L_3_A_L_3_1"   "RL_3_C_RL_3_9" "SL_3_A_SL_3_2"
akrun
  • 674,427
  • 24
  • 381
  • 486
  • $ tells to look at the last character whether it is letter, I guess, – amisos55 Apr 06 '21 at 17:07
  • 1
    @amisos55 i updated with description. Yes `$` speficies the end of string – akrun Apr 06 '21 at 17:07
  • 1
    amisos55, a good regex reference explains a lot of small regex-isms like that: https://stackoverflow.com/a/22944075/3358272. The biggest distinction is that most single-backslash references in regex need double-backslash in R; for example, regex `\D` becomes `\\D` in R. – r2evans Apr 06 '21 at 17:11
2

You can use the negative character class \\D, which matches anything that is not a digit:

sub("\\D$", "", string)
[1] "L_3_A_L_3_1"   "L_3_A_L_3_1"   "L_3_A_L_3_1"   "RL_3_C_RL_3_9" "SL_3_A_SL_3_2"
Chris Ruehlemann
  • 10,258
  • 2
  • 9
  • 18
2

Try this:

library(stringr)
str_remove(string,"[a-zA-Z]$")
Marcos Pérez
  • 1,210
  • 7