lundi 4 octobre 2021

Find pattern and wrap between parenthesis the next letter

I have to find different patterns in a data frame column, once it is found, the next letter should be wrapped between parentheses:

Data:

a <- c('(acetyl)RKJOEQLKQ', 'LDFEION(acetyl)EFNEOW')

if the pattern is: '(acetyl)' this is the output that I'd like to achieve:

Expected output:

b <- c('(R)KJOEQLKQ', 'LDFEION(E)FNEOW')

I know how that I can find the pattern with gsub:

b <- gsub('(acetyl)', replacement = '', a)

However, I'm not sure how to approach the wrapping between the parenthesis of the next letter after the pattern is found.

Any help would be appreciated.

Aucun commentaire:

Enregistrer un commentaire