Mark Breman
2010-Feb-11 19:39 UTC
[R] read numeric values with thousands seperator from csv file
Hello, Is there an easy way to read a csv file with numeric values that contain thousands seperators. The file looks like this: Date;opening;High;Low;closing;Volume 12/02/08;4,764.95;4,897.62;4,729.13;4,895.31;- 13/02/08;4,868.02;4,927.81;4,833.85;4,898.60;- 14/02/08;4,942.18;4,962.43;4,877.88;4,895.99;- I want to get the numeric values as..., well, numeric values, and not as character strings. I tried read.csv() with the colClasses parameter but that does not help and it looks like read.csv() does not support the big.mark="," parameter. Thanks, -Mark- [[alternative HTML version deleted]]
jim holtman
2010-Feb-11 19:53 UTC
[R] read numeric values with thousands seperator from csv file
Read them in as character and then convert them:> x <- c('4,123.45', '1,234,567.89') > x[1] "4,123.45" "1,234,567.89"> as.numeric(gsub(',', '', x))[1] 4123.45 1234567.89>On Thu, Feb 11, 2010 at 2:39 PM, Mark Breman <breman.mark at gmail.com> wrote:> Hello, > > Is there an easy way to read a csv file with numeric values that contain > thousands seperators. The file looks like this: > > Date;opening;High;Low;closing;Volume > 12/02/08;4,764.95;4,897.62;4,729.13;4,895.31;- > 13/02/08;4,868.02;4,927.81;4,833.85;4,898.60;- > 14/02/08;4,942.18;4,962.43;4,877.88;4,895.99;- > > I want to get the numeric values as..., well, numeric values, and not as > character strings. > > I tried read.csv() with the colClasses parameter but that does not help and > it looks like read.csv() does not support the big.mark="," parameter. > > Thanks, > > -Mark- > > ? ? ? ?[[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. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve?
Ista Zahn
2010-Feb-11 19:55 UTC
[R] read numeric values with thousands seperator from csv file
It's not particularly nice, but you could do Dat <- read.table(textConnection('Date;opening;High;Low;closing;Volume 12/02/08;4,764.95;4,897.62;4,729.13;4,895.31;- 13/02/08;4,868.02;4,927.81;4,833.85;4,898.60;- 14/02/08;4,942.18;4,962.43;4,877.88;4,895.99;-'), sep=";", header=TRUE, colClasses="character") Dat$opening <- as.numeric(gsub(",", "", Dat$opening)) Dat$High <- as.numeric(gsub(",", "", Dat$High)) Dat$Low <- as.numeric(gsub(",", "", Dat$Low)) Dat$closing <- as.numeric(gsub(",", "", Dat$closing)) -Ista On Thu, Feb 11, 2010 at 7:39 PM, Mark Breman <breman.mark at gmail.com> wrote:> Hello, > > Is there an easy way to read a csv file with numeric values that contain > thousands seperators. The file looks like this: > > Date;opening;High;Low;closing;Volume > 12/02/08;4,764.95;4,897.62;4,729.13;4,895.31;- > 13/02/08;4,868.02;4,927.81;4,833.85;4,898.60;- > 14/02/08;4,942.18;4,962.43;4,877.88;4,895.99;- > > I want to get the numeric values as..., well, numeric values, and not as > character strings. > > I tried read.csv() with the colClasses parameter but that does not help and > it looks like read.csv() does not support the big.mark="," parameter. > > Thanks, > > -Mark- > > ? ? ? ?[[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. >-- Ista Zahn Graduate student University of Rochester Department of Clinical and Social Psychology http://yourpsyche.org
Gabor Grothendieck
2010-Feb-11 20:21 UTC
[R] read numeric values with thousands seperator from csv file
Check this post: https://stat.ethz.ch/pipermail/r-help/2010-February/227520.html On Thu, Feb 11, 2010 at 2:39 PM, Mark Breman <breman.mark at gmail.com> wrote:> Hello, > > Is there an easy way to read a csv file with numeric values that contain > thousands seperators. The file looks like this: > > Date;opening;High;Low;closing;Volume > 12/02/08;4,764.95;4,897.62;4,729.13;4,895.31;- > 13/02/08;4,868.02;4,927.81;4,833.85;4,898.60;- > 14/02/08;4,942.18;4,962.43;4,877.88;4,895.99;- > > I want to get the numeric values as..., well, numeric values, and not as > character strings. > > I tried read.csv() with the colClasses parameter but that does not help and > it looks like read.csv() does not support the big.mark="," parameter. > > Thanks, > > -Mark- > > ? ? ? ?[[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. >