64

In R , what is the difference between read.csv() and read.csv2()

The official documentation says,

In various European locales, as the comma character serves as the decimal point, the function read.csv2 should be used instead

What does this mean. I don't see any difference at the superficial level. Can anybody give out a concrete example to clarify it further.

bartektartanus
  • 15,284
  • 6
  • 74
  • 102
Anoop
  • 5,540
  • 7
  • 35
  • 52

1 Answers1

66

They are (almost) the same functions - read.table. The only difference is default parameters. Look at source code:

> read.csv
function (file, header = TRUE, sep = ",", quote = "\"", dec = ".", 
    fill = TRUE, comment.char = "", ...) 
read.table(file = file, header = header, sep = sep, quote = quote, 
    dec = dec, fill = fill, comment.char = comment.char, ...)
<bytecode: 0x5e3fa88>
<environment: namespace:utils>
> read.csv2
function (file, header = TRUE, sep = ";", quote = "\"", dec = ",", 
    fill = TRUE, comment.char = "", ...) 
read.table(file = file, header = header, sep = sep, quote = quote, 
    dec = dec, fill = fill, comment.char = comment.char, ...)
<bytecode: 0x5c0a330>
<environment: namespace:utils>

From doc (see ?read.table):

read.csv and read.csv2 are identical to read.table except for the defaults. They are intended for reading ‘comma separated value’ files (‘.csv’) or (read.csv2) the variant used in countries that use a comma as decimal point and a semicolon as field separator.

bartektartanus
  • 15,284
  • 6
  • 74
  • 102
  • 2
    Thanks, that was helpful. I was not looking at the source code so far. – Anoop Apr 09 '14 at 17:54
  • 6
    It's possible to have comma be both the field separator `sep = ","` and also the decimal-separator `dec = ","`. But of course such fields will be quoted by the csv reader and writer. Anyway, such a file will still be 'real' csv (comma-separated). – Aaron McDaid Feb 26 '16 at 10:06