Search code examples
rdataframecharactersubstringsubstr

R - Conditional Substr from dataframe


I need to substr from a column based on start and end locations. The start and end locations are derived from a character search.

For example, a single column in Dataframe with 3 rows:

'Bond, Mr. :James'
'Woman, Mrs. :Wonder'
'Hood, Mr. :Robin'

Expected Answer in Column 2 is:

'Mr.'
'Mrs.'
'Mr.'

I want to extract all strings in between ',' and ':' for column 1.


Solution

  • Try gsub(".*, | :.*", "", myvec)