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"))
You can use str_remove
with a 'look ahead' part to detect following numbers:
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