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
                                         spec...@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

kevinw...@kpmg.com.au

Protect the environment: think before you print




        [[alternative HTML version deleted]]

______________________________________________
R-help@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@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.

Reply via email to