[R] Help with regular expressions

David Winsemius dwinsemius at comcast.net
Tue Feb 13 03:36:53 CET 2018


> On Feb 12, 2018, at 6:22 PM, Dennis Fisher <fisher at plessthan.com> wrote:
> 
> R 3.4.2
> OS X
> 
> Colleagues
> 
> I would appreciate some help with regular expressions.
> 
> I have string that looks like:
> 	" ITERATION              ,THETA1                 ,THETA2                 ,THETA3                 ,THETA4                 ,THETA5                 ,THETA6                 ,THETA7                 ,SIGMA(1,1)             ,SIGMA(2,1)             ,SIGMA(2,2)”
> 
> In the entries that contain:
> 	(X,Y)			# for example, SIGMA(1,1)
> I would like to replace the comma with a period, e.g., SIGMA(1.1) but NOT the other commas
> 
> The end-result would be:
> 	" ITERATION              ,THETA1                 ,THETA2                 ,THETA3                 ,THETA4                 ,THETA5                 ,THETA6                 ,THETA7                 ,SIGMA(1.1)             ,SIGMA(2.1)             ,SIGMA(2.2)”
> 
> Can someone provide the regular expression code to accomplish this?

gsub( "([(]\\d+)([,])(\\d+[)])", "\\1.\\3", x)\
#-----------
[1] "ITERATION              ,THETA1                 ,THETA2                 ,THETA3                 ,THETA4                 ,THETA5                 ,THETA6         ,THETA7                 ,SIGMA(1.1)             ,SIGMA(2.1)             ,SIGMA(2.2)"


> Thanks.
> 
> Dennis
> 
> Dennis Fisher MD
> P < (The "P Less Than" Company)
> Phone / Fax: 1-866-PLessThan (1-866-753-7784)
> www.PLessThan.com
> 
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

David Winsemius
Alameda, CA, USA

'Any technology distinguishable from magic is insufficiently advanced.'   -Gehm's Corollary to Clarke's Third Law



More information about the R-help mailing list