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
...
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
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
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
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
'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
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
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
8 matches
Mail list logo