[R] Help with gsub function

S Ellison S@E|||@on @end|ng |rom LGCGroup@com
Mon Mar 18 13:31:50 CET 2019


> 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:8}}



More information about the R-help mailing list