Update column entries to remove the last character if it's an underscore

I want to remove the underscore "_" from the column entries of col1 only when the underscore is the last character.

Example:

data1 <- c("foo_bar_","bar_foo","apple_","apple__beer_")
df <- data.frame("col1"=data1,"col2"=1:4)
df
        col1   col2
    foo_bar_      1
     bar_foo      2
      apple_      3
apple__beer_      4

Desired output:

        col1   col2
     foo_bar      1
     bar_foo      2
       apple      3
 apple__beer      4

Thank you in advance for your time and help!

>Solution :

We can use sub() here for a base R option:

df$col1 <- sub("_$", "", df$col1)
df

         col1 col2
1     foo_bar    1
2     bar_foo    2
3       apple    3
4 apple__beer    4

Leave a Reply