[R] simple parsing question?
Fuchs Ira
irafuchs at gmail.com
Thu Oct 11 23:06:33 CEST 2012
Yes, in my case it would be re-learning regular expressions. Unlike riding a bicycle, this is something I have managed to forget (except for the simplest cases). I even have an old O'reilly book on the subject which I can dust off. I was thinking (hoping?) that quantmod had functions to manipulate the information returned by Yahoo but I guess that is not the case. Anyway thanks to everyone's help, I now know how to proceed.
Best,
Ira
On Oct 11, 2012, at 4:59 PM, Bert Gunter wrote:
> Just a comment.
>
> On Thu, Oct 11, 2012 at 1:45 PM, Fuchs Ira <irafuchs at gmail.com> wrote:
>> 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.**
> Actually, it's not "R's sub function," it's regular expressions, which
> are independent of R and used in many other languages for text
> processing. They also have an interesting history in computer science.
> You might wish to have a look at Wikipedia's or other source's page on
> regular expressions to get some background. Depending on the nature of
> your work, you may also wish to reconsider your avoidance of learning
> the regular expression syntax, which is, however, a chore.
>
> Best,
> Bert
>
>
>
> 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 (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.
>>
>> ______________________________________________
>> 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.
>
>
>
> --
>
> Bert Gunter
> Genentech Nonclinical Biostatistics
>
> Internal Contact Info:
> Phone: 467-7374
> Website:
> http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm
More information about the R-help
mailing list