Back

Explore Courses Blog Tutorials Interview Questions
0 votes
1 view
in R Programming by (7.3k points)

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

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))})

Welcome to Intellipaat Community. Get your technical queries answered by top developers!

28.4k questions

29.7k answers

500 comments

94.2k users

Browse Categories

...