[R] Converting dollar value (factors) to numeric

Fredrik Karlsson dargosch at gmail.com
Wed May 5 09:54:55 CEST 2010


Hi,

Something similar to this maybe?

> test <- as.factor("$111.11")
> test
[1] $111.11
Levels: $111.11
> as.numeric(substring(as.character(test),2))
[1] 111.11

To be applied to your data.frame columns.

/Fredrik

On Wed, May 5, 2010 at 9:16 AM, Wang, Kevin (SYD) <kevinwang at kpmg.com.au> 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.
>



-- 
"Life is like a trumpet - if you don't put anything into it, you don't
get anything out of it."



More information about the R-help mailing list