[R] simple parsing question?

William Dunlap wdunlap at tibco.com
Thu Oct 11 22:27:28 CEST 2012


> qs <- getQuote(c("aapl","tibx","gm","badWolf"),what=yahooQF(c("Change Percent (Real-time)")))
> qs
                 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 Behalf
> 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 for
> a stock as follows:
> 
> > 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.
> ______________________________________________
> R-help at r-project.org mailing list
> 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.




More information about the R-help mailing list