[R] Help with gsub function

Bill Poling B|||@Po||ng @end|ng |rom ze||@@com
Mon Mar 18 15:04:33 CET 2019


Good morning Steve. Terrific, so kind of you to follow-up.

I will add that to my ever growing R bag of tips and tricks.

Cheers.

WHP

William H. Poling, Ph.D., MPH | Manager, Revenue Development
Data Intelligence & Analytics
Zelis Healthcare


-----Original Message-----
From: S Ellison <S.Ellison using LGCGroup.com>
Sent: Monday, March 18, 2019 8:32 AM
To: Bill Poling <Bill.Poling using zelis.com>; r-help (r-help using r-project.org) <r-help using r-project.org>
Subject: RE: Help with gsub function

> tb2a$TID2 <- gsub(tb2a$TID, pattern="-[0-0]{0,7}", replacement = "")

Just to add something on why this didn't work ...

It looks like you were trying to match a hyphen followed by a number up to seven digits. by mistake(?) you gave the digit range as [0-0] so it would repmatch a hyphen followed by between none and seven zeroes. When it met "-0" it matched that.
And because it was gsub, it replaced what it matched.
If you'd given it the right digit range it would have replaced the whole of the number.

If you _really_ wanted to do that kind of thing (control the following pattern), you'd have needed something like (untested) gsub("-([0-0]{0,7})", "\\1", tb2a$TID)

#The () means 'remember this bit"; the "\\1" means "put the first thing you remember here". And it needs to be "\\1" because that becomes "\1" for the grep parser.


Steve E


*******************************************************************
This email and any attachments are confidential. Any use...{{dropped:22}}



More information about the R-help mailing list