Rob Hyndman Rob Hyndman - 1 year ago 113
R Question

How to read data when some numbers contain commas as thousand separator?

I have a csv file where some of the numerical values are expressed as strings with commas as thousand separator, e.g.

instead of
. What is the simplest way to read the data into R?

I can use
read.csv(..., colClasses="character")
, but then I have to strip out the commas from the relevant elements before converting those columns to numeric, and I can't find a neat way to do that.

Answer Source

I want to use R rather than pre-processing the data as it makes it easier when the data are revised. Following Shane's suggestion of using gsub, I think this is about as neat as I can do:

x <- read.csv("file.csv",header=TRUE,colClasses="character")
col2cvt <- 15:41
x[,col2cvt] <- lapply(x[,col2cvt],function(x){as.numeric(gsub(",", "", x))})
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download