Skip to content
Prev 219756 / 398500 Next

Converting dollar value (factors) to numeric

Hi Phil and all those who replied,

Thanks heap!  Yes it worked to a certain extent.  However, if I have the
following case:
Warning message:
NAs introduced by coercion
[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:
[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:

            
factors.