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.
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
Hi r-help-bounces at r-project.org napsal dne 24.02.2010 12:15:04:> > I'm struggling to find any help on this seemingly simple question - howdoes> one read data with percentage (%) or currency (?,$ etc.) signs? When Itry> 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 orlibrary> which can deal with such values?One option is read them as they are. You will get factors (or character data with correct parameters setting) as.numeric(Sale1, 1, nchar(Sale1-1)) You shall wrap this into lapply/sapply if you want to apply it to columns of data frame. Regards Petr> > 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 makeR-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 guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.