I have a dataframe with 1000 rows and 37 columns and would like to drop the first word from the string including the underscore and keep remainder of the string:
example
Column A Column B Column C
Mid-size colourblind_RED_Pglasses_Vision xxx xxx
High-size cannotEat_JAM_Pots_Jammie yyy yyy
I want to drop the first word from the string including the underscore and keep remainder of the string for column A eg. RED_Pglasses_Vision.
CodePudding user response:
You can use lookaround:
library(stringr)
str_extract(x, "(?<=_).*")
where x
is you vector with the strings and (?<=_)
is lookbehind asserting that the match must start after the first underscore
CodePudding user response:
We could use trimws
from base R
trimws(str1, whitespace = "[^_] _")
[1] "RED_Pglasses_Vision" "JAM_Pots_Jammie"
data
str1 <- c("Mid-size colourblind_RED_Pglasses_Vision", "High-size cannotEat_JAM_Pots_Jammie")