[R] How to read percentage and currency data?
Ista Zahn
istazahn at gmail.com
Wed Feb 24 12:59:31 CET 2010
There might be a package that will do what you want, but probably the
simplest solution is to remove the unit symbols:
Dat <- read.table(textConnection("Store Year Revenue
abc 2010 $557889
def 2010 $697356"), header=TRUE, stringsAsFactors=FALSE)
closeAllConnections()
Dat$Revenue <- as.numeric(gsub("\\$", "", Dat$Revenue))
It's easy enough to past it back in for display purposes, e.g.:
paste("$", mean(Dat$Revenue), sep="")
-Ista
On Wed, Feb 24, 2010 at 6:15 AM, Hrishi Mittal <hrishimittal at gmail.com> wrote:
>
> I'm struggling to find any help on this seemingly simple question - how does
> one read data with percentage (%) or currency (£,$ etc.) signs? When I try
> to read a data file which has any of those symbols in the data fields, they
> are read as characters rather than values. Is there a function or library
> which can deal with such values?
>
> As an example, I use this sample from one of chinna's questions:
>
> Store Year Revenue
> abc 2010 $557889
> def 2010 $697356
>
> Similarly, for percentage values:
>
> Product Sale1 Sale2 Sale3 Sale4 Sale5 Sale6 Sale7
> A 101.00% 93.00% 85.00% 65.00% 74.00% 102.00% 119.00%
> B 585.00% 484.00% 599.00% 653.00% 726.00% 882.00% 1035.00%
> C 18.00% 19.00% 25.00% 15.00% 31.00% 33.00% 33.00%
> D 23.00% 22.00% 31.00% 30.00% 36.00% 42.00% 49.00%
> E 35.00% 41.00% 49.00% 40.00% 48.00% 48.00% 53.00%
> F 19.00% 20.00% 33.00% 16.00% 20.00% 26.00% 28.00%
>
>
> -----
> Try http://prettygraph.com Pretty Graph , the easiest way to make R-powered
> graphs on the web.
> --
> View this message in context: http://n4.nabble.com/How-to-read-percentage-and-currency-data-tp1567318p1567318.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> 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.
>
--
Ista Zahn
Graduate student
University of Rochester
Department of Clinical and Social Psychology
http://yourpsyche.org
More information about the R-help
mailing list