Search code examples
rstringstringr

string remove pattern with special character in R


I have an object in R called text :

text = c(" - John - Loves Helen - Day 4")  

I want to remove the starting space the "-" and the space (i.e) all the special characters before the first word.

ideally I want to the result to be :

John - Loves Helen - Day 4

please take into account that in my real data set the column with text might contain different starting word (i.e different from John).

How can I do that in R ?


Solution

  • This should work:

    text = c(" - John - Loves Helen - Day 4") 
    
    text.split <- unlist(strsplit(text,split = " "))
    
    text.split <- text.split[-c(1,2)]
    
    text.split <- paste(text.split,collapse = " ")
    
    print(text.split)
    
    "John - Loves Helen - Day 4"
    

    This also works and is neater:

    text <- sub("^\\W*","",text)
    
    print(text)
    
    "John - Loves Helen - Day 4"
    

    Hope it helps!