Search code examples
rtidyverse

How to remove the last "-" in a text string when a number follows it


I have multiple text columns with IDs. I want to remove the "-" only if it is the last one and the following character is a number. Please show tidyverse syntax as I will be mutating across multiple columns.

EX

data <- data.frame(id = c("2022-05-19-admin-raca-16", "2022-05-19-admin-hyla16"))

Desired Outputs

correct_data <- data.frame(id = c("2022-05-19-admin-raca16", "2022-05-19-admin-hyla16"))

Solution

  • You can use str_remove with a 'look ahead' part to detect following numbers:

    • "-" is the part you want to remove
    • "(?=\\d+$)" means 'followed by one or more digits at the end of the string' (but leave these there).
    library(tidyverse)
    
    data <-
      data.frame(id = c("2022-05-19-admin-raca-16", "2022-05-19-admin-hyla16"))
    
    data |>
      mutate(id = str_remove(id, "-(?=\\d+$)"))
    #>                        id
    #> 1 2022-05-19-admin-raca16
    #> 2 2022-05-19-admin-hyla16