-----Original Message-----
From: arun [mailto:smartpink111 at yahoo.com]
Sent: Thursday, October 11, 2012 1:54 PM
To: Fuchs Ira
Cc: R help; William Dunlap
Subject: Re: [R] simple parsing question?
HI,
Try this:
?sprintf("%.2f",as.numeric(sub("^.* ([-+]?[[:digit:].]+)%$", "\\1", as.character(aapl[[2]]))))
#[1] "-2.00"
A.K.
----- Original Message -----
From: Fuchs Ira <irafuchs at gmail.com>
To: r-help at r-project.org
Cc:
Sent: Thursday, October 11, 2012 4:45 PM
Subject: Re: [R] simple parsing question?
I'm glad I asked as I would have thought that this was a common requirement and
quantmod itself or a simple R function would have done the conversion. You saved me
from having to master R's sub function. One remaining thing?when I use your snippet for
AAPL, I get:
aapl=getQuote("aapl",what=yahooQF(c("Change Percent (Real-time)")))
as.numeric(sub("^.* ([-+]?[[:digit:].]+)%$", "\\1", as.character(aapl[[2]])))
[1] -2
not the -2.00 that you got. Do I have a setting that is causing it to not show the
significant digits?
Thanks.
On Oct 11, 2012, at 4:27 PM, William Dunlap wrote:
qs <- getQuote(c("aapl","tibx","gm","badWolf"),what=yahooQF(c("Change Percent
? ? ? ? ? ? ? ? Trade Time %Change (RT)
aapl? ? 2012-10-11 04:00:00 N/A - -2.00%
tibx? ? 2012-10-11 04:00:00 N/A - -0.85%
gm? ? ? 2012-10-11 04:00:00 N/A - +1.77%
badWolf? ? ? ? ? ? ? ? <NA>? N/A - 0.00%
as.numeric(sub("^.* ([-+]?[[:digit:].]+)%$", "\\1", as.character(qs[[2]])))
[1] -2.00 -0.85? 1.77? 0.00
The "\\1" in the replacement argument to sub() means the
text matched by the first parenthesized subpattern in the pattern
argument.
Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com
-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On
Of Fuchs Ira
Sent: Thursday, October 11, 2012 12:58 PM
To: r-help at r-project.org
Subject: [R] simple parsing question?
I am using the getQuote function in the Quantmod package to retrieve the % change
getQuote("aapl",what=yahooQF(c("Change Percent (Real-time)")))
? ? ? ? ? ? Trade Time %Change (RT)
aapl 2012-10-11 03:41:00 N/A - -1.67%
How can I extract the numeric "change %" which is being returned as a factor so that I
can use it in other calculations?
Thanks.