I am struggling to remove the substring before the underscore in my string. I want to use * (wildcard) as the bit before the underscore can vary:
a <- c(\
Just to point out that there is an approach using functions from the tidyverse
, which I find more readable than gsub
:
a %>% stringr::str_remove(pattern = ".*_")
The following code works on your example :
gsub(".*_", "", a)
Alternatively, you can also try:
gsub("\\S+_", "", a)
as.numeric(gsub(pattern=".*_", replacement = '', a)
[1] 5 7