Remove parts of string based on other column in R

Probably a simple question for many of you... I want to remove a part of a string (based on another column) and am interested in the remainder of the string...

Example:

  dt <- data.frame(SomeText = c("ABCDEF", "ABCDEF", "ABCDEF"), 
                   ToRemove = c("A", "CDE", ""), 
                   WantedResult = c("BCDEF", "ABF", "ABCDEF"))

>   dt
  SomeText ToRemove WantedResult
1   ABCDEF        A        BCDEF
2   ABCDEF      CDE          ABF
3   ABCDEF                ABCDEF

Thus (line 2 as example), remove 'CDE' from 'ABCDEF' so we are left with 'ABF'

Cheers, Luc



Read more here: https://stackoverflow.com/questions/65713337/remove-parts-of-string-based-on-other-column-in-r

Content Attribution

This content was originally published by Luc at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: