[R] Converting dollar value (factors) to numeric
Wang, Kevin (SYD)
kevinwang at kpmg.com.au
Thu May 6 05:31:29 CEST 2010
Hi Phil and all those who replied,
Thanks heap! Yes it worked to a certain extent. However, if I have the
following case:
> x <- c("$135,359.00", "$135359.00", "$1,135,359.00")
> y <- sub('\\$','',as.character(x))
> cost <- as.numeric(sub('\\,','',as.character(y)))
Warning message:
NAs introduced by coercion
> cost
[1] 135359 135359 NA
Then the third value bcomes NA -- though I suspect it's probably has
something to do with regular expression (which I'm not sure how to fix)
than R?
Thanks again for the help!
Cheers
Kev
-----Original Message-----
From: Phil Spector [mailto:spector at stat.berkeley.edu]
Sent: Wednesday, 5 May 2010 6:14 PM
To: Wang, Kevin (SYD)
Cc: r-help at r-project.org
Subject: Re: [R] Converting dollar value (factors) to numeric
Kev-
The most reliable way to do the conversion is as follows:
> x = factor(c('$112.11','$119.15','$121.32'))
> as.numeric(sub('\\$','',as.character(x)))
[1] 112.11 119.15 121.32
This way negative quantities and numbers without dollar signs are
handled correctly. There's certainly no need to create a new input
file.
It may be easier to understand as
as.numeric(sub('$','',as.character(x),fixed=TRUE))
which gives the same result.
- Phil Spector
Statistical Computing Facility
Department of Statistics
UC Berkeley
spector at stat.berkeley.edu
On Wed, 5 May 2010, Wang, Kevin (SYD) wrote:
> Hi,
>
> I'm trying to read in a bunch of CSV files into R where many columns
> are coded like $111.11. When reading them in they are treated as
factors.
>
> I'm wondering if there is an easy way to convert them into numeric in
> R (as I don't want to modify the source data)? I've done some
> searches and can't seem to find an easy way to do this.
>
> I apologise if this is a trivial question, I haven't been using R for
> a while.
>
> Many thanks in advance!
>
> Cheers
>
> Kev
>
> Kevin Wang
>> Senior Advisor, Health and Human Services Practice Government
>> Advisory Services
>>
>> KPMG
>> 10 Shelley Street
>> Sydney NSW 2000 Australia
>>
>> Tel +61 2 9335 8282
>> Fax +61 2 9335 7001
>>
> kevinwang at kpmg.com.au
>
>> Protect the environment: think before you print
>>
>>
>
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> 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