[R] Odp: How to read percentage and currency data?

David Winsemius dwinsemius at comcast.net
Thu Feb 25 00:45:41 CET 2010


On Feb 24, 2010, at 6:10 PM, Gabor Grothendieck wrote:

> On Wed, Feb 24, 2010 at 11:28 AM, Petr PIKAL  
> <petr.pikal at precheza.cz> wrote:
>> Hi
>>
>> r-help-bounces at r-project.org napsal dne 24.02.2010 16:55:49:
>>
>>>
>>> Petr,
>>>
>>> Removing the % signs by Ista's method works. However as I noted  
>>> earlier, I
>>> was hoping there was a more elegant solution which deals with  
>>> percentage and
>>> currency values without knowing about their presence beforehand.
>>>
>>> It's a shame that something which Excel deals with trivially is  
>>> such a hack
>>> in R. I'll try to delve deeper and write something which does this  
>>> well. But
>>> my knowledge is limited and I will probably come back for help.
>>
>> I believe that if you introduce a new class which could specify  
>> currencies
>> you could write methods for handling them. But it is beyond my  
>> knowledge
>> and somebody else has to answer it.
>>
>
> Try adapting this example of using a custom class with read.table:
>
>  https://www.stat.math.ethz.ch/pipermail/r-help/2007-April/130912.html

So this seems a bit simplistic. There is nothing in the way of  
attributes to leave a trail of conversions:

 > setAs("character", "num.with.commas",
+    function(from) as.numeric(gsub(",", "", from)))
 > setAs("character", "euro",
+    function(from) as.numeric(gsub("€", "", from)))
 > setAs("character", "num_pct",
+    function(from) as.numeric(gsub("%", "", from))/100)

 > Input <- "A B C
+ 1,000 1% 3.50€
+ 2,000 2% 4.77€
+ 3,000 3% €5.68
+ "
 > DF <- read.table(textConnection(Input), header = TRUE,
+    colClasses = c("num.with.commas", "num_pct", "euro"))
 > str(DF)
'data.frame':	3 obs. of  3 variables:
  $ A: num  1000 2000 3000
  $ B: num  0.01 0.02 0.03
  $ C: num  3.5 4.77 5.68


>
> or this example of piping through tr to read.table:
>
>  http://tolstoy.newcastle.edu.au/R/e9/help/10/02/4324.html
>
> ______________________________________________
> 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.

David Winsemius, MD
Heritage Laboratories
West Hartford, CT



More information about the R-help mailing list