Re: [R] Regular Expressions in grep - Solution and function to determine significant figures of a number

2012-08-23 Thread Dr. Holger van Lishaut
Am 22.08.2012, 21:46 Uhr, schrieb Dr. Holger van Lishaut : SignifStellen<-function(x){ strx=as.character(x) nchar(regmatches(strx, regexpr("[1-9][0-9]*\\.[0-9]*[1-9]",strx)))-1 } returns the significant figures of a number. Perhaps this can help someone. Sorry, to work, it must

Re: [R] Regular Expressions in grep - Solution and function to determine significant figures of a number

2012-08-22 Thread Bert Gunter
... On Wed, Aug 22, 2012 at 12:46 PM, Dr. Holger van Lishaut wrote: > Dear all, > > regmatches works. > > And, since this has been asked here before: > > SignifStellen<-function(x){ > strx=as.character(x) > nchar(regmatches(strx, regexpr("[1-9][0-9]*\\.[0-9]*[1-9]",strx)))-1 > } > > retur

Re: [R] Regular Expressions in grep - Solution and function to determine significant figures of a number

2012-08-22 Thread Dr. Holger van Lishaut
Dear all, regmatches works. And, since this has been asked here before: SignifStellen<-function(x){ strx=as.character(x) nchar(regmatches(strx, regexpr("[1-9][0-9]*\\.[0-9]*[1-9]",strx)))-1 } returns the significant figures of a number. Perhaps this can help someone. Thanks & best reg

Re: [R] Regular Expressions in grep

2012-08-21 Thread arun
HI, Try this: gsub("^-\\d(\\d{4}.).*","\\1",a) #[1] "1020." gsub("^.*(.\\d{5}).","\\1",a) #[1] ".90920" A.K. - Original Message - From: Dr. Holger van Lishaut To: "r-help@r-project.org" Cc: Sent: Tuesday, August 2

Re: [R] Regular Expressions in grep

2012-08-21 Thread R. Michael Weylandt
You're misreading the docs: from grep, value: if ‘FALSE’, a vector containing the (‘integer’) indices of the matches determined by ‘grep’ is returned, and if ‘TRUE’, a vector containing the matching elements themselves is returned. Since there's a match somewhere

Re: [R] Regular Expressions in grep

2012-08-21 Thread Noia Raindrops
'grep' does not change strings. Use 'gsub' or 'regmatches': # gsub Front <- gsub("^.*?([1-9][0-9]*\\.).*?$", "\\1", a) End <- gsub("^.*?(\\.[0-9]*[1-9]).*?$", "\\1", a) # regexpr and regmatches (R >= 2.14.0) Front <- regmatches(a, regexpr("[1-9][0-9]*\\.", a)) End <- regmatches(a, regexpr("\\.[0-9

Re: [R] Regular Expressions in grep

2012-08-21 Thread Bert Gunter
grep() returns the matches. You want regexpr() and regmatches() -- Bert On Tue, Aug 21, 2012 at 12:24 PM, Dr. Holger van Lishaut wrote: > Dear r-help members, > > I have a number in the form of a string, say: > > a<-"-01020.909200" > > I'd like to extract "1020." as well as ".9092" > > Front<-gr

[R] Regular Expressions in grep

2012-08-21 Thread Dr. Holger van Lishaut
Dear r-help members, I have a number in the form of a string, say: a<-"-01020.909200" I'd like to extract "1020." as well as ".9092" Front<-grep(pattern="[1-9]+[0-9]*\\.", value=TRUE, x=a, fixed=FALSE) End<-grep(pattern="\\.[0-9]*[1-9]+", value=TRUE, x=a, fixed=FALSE) However, both strings gi