Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in R Programming by (7.3k points)
edited by

I have a CSV file where some of the numerical values are expressed as strings with commas as thousand separators, e.g. "1,513" instead of 1513. 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

1 Answer

0 votes
by
edited by

To read data when some numbers contain commas as a separator, you can use the gsub function to firstly replace the commas and then use as.numeric function to convert it to a numeric type.

For example:

v <- c("1,500","35,699","1200","58,711")

>  as.numeric(gsub(",", "", v))

[1]  1500 35699  1200 58711

In your case:

If you want to read columns from 5 to 10 that contain commas as a separator, you would do something like this:

 x <- read.csv("file.csv",header=TRUE,colClasses="character")

 cols <- 5:10

 x[,cols] <- lapply(x[,cols],function(x){as.numeric(gsub(",", "", x))})

Browse Categories

...