Skip to content
Prev 307710 / 398506 Next

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:
[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:

            
______________________________________________
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.