Home > Back-end >  Update column entries to remove the last character if it's an underscore
Update column entries to remove the last character if it's an underscore

Time:11-23

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!

CodePudding user response:

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

CodePudding user response:

For a tidyverse solution use stringr::str_replace:

library(dplyr)
library(stringr)

df %>%
  mutate(col1 = str_replace(col1, "_$", ""))

#          col1 col2
# 1     foo_bar    1
# 2     bar_foo    2
# 3       apple    3
# 4 apple__beer    4
  • Related