Hi
Given that things like the following work
> a <- c("-.1"," 2.7
","B")> a
[1] "-.1" " 2.7 " "B"
> as.numeric(a)
[1] -0.1 2.7 NA
Warning message:
NAs introduced by coercion >
I naively expected that the following would behave differently.
> b <- c('10%', '-20%', '30.0%',
'.40%')> b
[1] "10%" "-20%" "30.0%" ".40%"
> as.numeric(b)
[1] NA NA NA NA
Warning message:
NAs introduced by coercion >
> version
_
platform i386-pc-mingw32
arch i386
os mingw32
system i386, mingw32
status
major 2
minor 2.0
year 2005
month 10
day 06
svn rev 35749
language R >
Various RSiteSearches with terms like "percentage" and
"coercion" yielded
nothing.
Does anyone know how to do this elegantly?
Thanks
Tobias Brandt
P.S. Apologies if this appears on the list twice but I suspect an earlier
post was blocked since it was in html format.
********************
Nedbank Limited Reg No 1951/000009/06
Directors: WAM Clewlow (Chairman) Prof MM Katz (Vice-chairman) ML Ndlovu
(Vice-chairman) TA Boardman (Chief Executive)
CJW Ball MWT Brown RG Cottrell BE Davison N Dennis (British) MA Enus-Brey
Prof B de L Figaji RM Head (British)
RJ Khoza JB Magwaza ME Mkwanazi JVF Roberts (British) CML Savage GT Serobe
JH Sutcliffe (British)
Company Secretary: GS Nienaber 16.08.2005
This email and any accompanying attachments may contain confidential and
proprietary information. This information is private and protected by law and,
accordingly, if you are not the intended recipient, you are requested to delete
this entire communication immediately and are notified that any disclosure,
copying or distribution of or taking any action based on this information is
prohibited.
Emails cannot be guaranteed to be secure or free of errors or viruses. The
sender does not accept any liability or responsibility for any interception,
corruption, destruction, loss, late arrival or incompleteness of or tampering or
interference with any of the information contained in this email or for its
incorrect delivery or non-delivery for whatsoever reason or for its effect on
any electronic device of the recipient.
If verification of this email or any attachment is required, please request a
hard-copy version.
On 11/14/05, Brandt, T. (Tobias) <TobiasBr at taquanta.com> wrote:> Hi > > Given that things like the following work > > > a <- c("-.1"," 2.7 ","B") > > a > [1] "-.1" " 2.7 " "B" > > as.numeric(a) > [1] -0.1 2.7 NA > Warning message: > NAs introduced by coercion > > > > I naively expected that the following would behave differently. > > > b <- c('10%', '-20%', '30.0%', '.40%') > > b > [1] "10%" "-20%" "30.0%" ".40%" > > as.numeric(b) > [1] NA NA NA NA > Warning message: > NAs introduced by coercionTry this: as.numeric(sub("%", "e-2", b))
>-----Original Message----- >From: Gabor Grothendieck [mailto:ggrothendieck@gmail.com] >Sent: 14 November 2005 06:21 PM > >On 11/14/05, Brandt, T. (Tobias) <TobiasBr@taquanta.com> wrote: >> Hi >> >> Given that things like the following work >> >> > a <- c("-.1"," 2.7 ","B") >> > a >> [1] "-.1" " 2.7 " "B" >> > as.numeric(a) >> [1] -0.1 2.7 NA >> Warning message: >> NAs introduced by coercion >> > >> >> I naively expected that the following would behave differently. >> >> > b <- c('10%', '-20%', '30.0%', '.40%') >> > b >> [1] "10%" "-20%" "30.0%" ".40%" >> > as.numeric(b) >> [1] NA NA NA NA >> Warning message: >> NAs introduced by coercion > >Try this: > >as.numeric(sub("%", "e-2", b)) >Thank you, that accomplishes what I had intended. I would have thought though that the expression "53%" would be a fairly standard representation of the number 0.53 and might be handled as such. Is there a specific reason for avoiding this behaviour? I can imagine that it might add unnecessary overhead to routines like "as.numeric" which one would like to keep as fast as possible. Perhaps there are other areas though where it might be desirable? For example I'm thinking of the read.table function for reading in csv files since I have many of these that have been saved from excel and now contain numbers in the "%" format. ******************** Nedbank Limited Reg No 1951/000009/06 Directors: WAM Clewlow (Chairman) Prof MM Katz (Vice-chairman) ML Ndlovu (Vice-chairman) TA Boardman (Chief Executive) CJW Ball MWT Brown RG Cottrell BE Davison N Dennis (British) MA Enus-Brey Prof B de L Figaji RM Head (British) RJ Khoza JB Magwaza ME Mkwanazi JVF Roberts (British) CML Savage GT Serobe JH Sutcliffe (British) Company Secretary: GS Nienaber 16.08.2005 This email and any accompanying attachments may contain confidential and proprietary information. This information is private and protected by law and, accordingly, if you are not the intended recipient, you are requested to delete this entire communication immediately and are notified that any disclosure, copying or distribution of or taking any action based on this information is prohibited. Emails cannot be guaranteed to be secure or free of errors or viruses. The sender does not accept any liability or responsibility for any interception, corruption, destruction, loss, late arrival or incompleteness of or tampering or interference with any of the information contained in this email or for its incorrect delivery or non-delivery for whatsoever reason or for its effect on any electronic device of the recipient. If verification of this email or any attachment is required, please request a hard-copy version. ******************** [[alternative HTML version deleted]]