Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I declare a thousand separator in read.csv? [duplicate]

Tags:

r

csv

The dataset I want to read in contains numbers with and without a comma as thousand separator:

"Sudan", "15,276,000", "14,098,000", "13,509,000"
"Chad", 209000, 196000, 190000

and I am looking for a way to read this data in.

Any hint appreciated!

like image 848
Karsten W. Avatar asked Feb 27 '10 13:02

Karsten W.


1 Answers

since there is an "r" tag under the question, I assume this is an R question. In R, you do not need to do anything to handle the quoted commas:

> read.csv('t.csv', header=F)
     V1          V2          V3          V4
1 Sudan  15,276,000  14,098,000  13,509,000
2  Chad      209000      196000      190000

# if you want to convert them to numbers:
> df <- read.csv('t.csv', header=F, stringsAsFactor=F)
> df$V2 <- as.numeric(gsub(',', '', df$V2))
like image 192
xiechao Avatar answered Oct 09 '22 17:10

xiechao