📅  最后修改于: 2022-03-11 14:52:00.753000             🧑  作者: Mango
# You could use stringr::str_extract:
library(stringr)
df %>%
dplyr::mutate(new_id = str_extract(id, "[^_]+$"))
#> id x new_id
#> 1 abcd_123_ABC 1 ABC
#> 2 abc_5234_NHYK 2 NHYK
# The regex says, match one or more (+) of the characters that aren't _ (the negating [^ ]), followed by end of string ($).