-1

Is there a function in R that can respond at these requirements:

if string1 exists in string2 then remove string1 from string2

I passed a day searching on a such function. So, any help would be appreciated.

Edit:

I have a dataframe. Here's a part of it:

mark      name                                ChekMark
Caudalie  Caudalie Eau démaquillante 200ml    TRUE
Mustela   Mustela Bébé lait hydra corps 300ml TRUE
Lierac    Lierac Phytolastil gel prévention   TRUE

I want to create an new dataframe in witch the mark doesn't exist on the product name.

That's my final goal.

sarah
  • 219
  • 5
  • 12

2 Answers2

1

Are you looking for string2.replace(string1, '')?

or you could:

>>> R = lambda string1, string2: string2.replace(string1, '')
>>> R('abc', 'AAAabcBBB')
'AAABBB'
>>>
Quinn
  • 4,216
  • 2
  • 19
  • 18
  • Yes I'm looking for a something like this. Any idea ? – sarah Jan 18 '16 at 14:45
  • `gsub` and `mapply` resolved the problem : `df_mod=as.data.frame(mapply(gsub,data_mod$marq,"",data_mod$nom_det))`. Thank you for your time and your efforts. – sarah Jan 20 '16 at 09:23
1

You can use gsub and work with regular expressions:

gsub(" this part ", " ", "A Text where this part should be removed")
# [1] "A Text where should be removed"
gsub(" this part ", " ", "A Text where this 1 part should be removed")
# [1] "A Text where this 1 part should be removed"
thothal
  • 13,113
  • 1
  • 29
  • 65
  • I switched my dataframe to a list and I tried to use `gsub`, but It doesn't work for me: `lst – sarah Jan 18 '16 at 15:19
  • `gsub` and `mapply` resolved the problem : `df_mod=as.data.frame(mapply(gsub,data_mod$marq,"",data_mod$nom_det))`. Thank you for your time and your efforts. – sarah Jan 20 '16 at 09:20