[R] read.csv data frame thousands separator

Barry Rowlingson b.rowlingson at lancaster.ac.uk
Fri Apr 23 15:14:42 CEST 2010

On Fri, Apr 23, 2010 at 2:00 PM, arnaud Gaboury
<arnaud.gaboury at gmail.com> wrote:
> Dear group,

> "2,421.5000", "2,448.5000", "1,380.0000", "1,383.0000", "1,383.0000",
> "1,386.0000", "1,386.0000", "1,388.0000", "1,389.0000", "1,389.0000"

> The csv file has some numbers with thousands separator as a ".", so the
> class for my "Price" column is character, when I want it numeric.

 Looks like the thousands separator is a comma rather than a dot - the
dot is the decimal point separator. Or does your data have some commas
and some dots?

> I can't change the column class, even when playing with the read.csv or
> as.numeric commands and all their arguments.

 If it's consistent, ie all commas for thousands, then just use the
gsub function to replace commas with nothing and then as.numeric:

> as.numeric(gsub(",","","1,234,567.333"))
[1] 1234567

 so in your data frame:

 foo$N = as.numeric(gsub(",","",foo$N))

 there's probably a proper way of doing this involving
internationalisation settings and so forth, but this should be a quick
and dirty way of doing it.


More information about the R-help mailing list