set separator ';' in write.csv
Asked Answered
M

3

14

I have the following code:

myTable[i,] = strsplit(line, split=";")[[1]]
write.csv(myTable[-1,], file="episodes_cleared.csv", sep=";", row.names=FALSE, quote=FALSE)

Unfortunately, the separator still is ',':

iEpisodeId,iPatientId,sTitle,sICPc,dStart,dEnd,bProblem

Running the code gives me:

Warning messages:

1: In write.csv(myTable[-1, ], file = "episodes_cleared.csv", sep = ";",  : attempt to set 'sep' ignored
2: In write.csv(myTable[-1, ], file = "episodes_cleared.csv", sep = ";",  :

attempt to set 'sep' ignored

What am I doing wrong?

Militant answered 23/6, 2016 at 19:23 Comment(3)
Try write.table (CSV stands for Comma Seperated Value BTW).Checkoff
write.csv2 defaults to sep = ";" (and dec = ",", which is why semicolon separators are sometimes used).Withe
there is no actual specification for what a CSV is. Comma is only the implied delimiter. It needn't be the delimiter used.Mathamathe
R
23

First, you should provide a reproducible example.

However, if you use write.csv2 it defaults to using a semicolon as the separator.

Riband answered 23/6, 2016 at 19:28 Comment(0)
M
11

The documentation https://stat.ethz.ch/R-manual/R-devel/library/utils/html/write.table.html says:

write.csv uses "." for the decimal point and a comma for the separator.

write.csv2 uses a comma for the decimal point and a semicolon for the separator, the Excel convention for CSV files in some Western European locales.

These wrappers are deliberately inflexible: they are designed to ensure that the correct conventions are used to write a valid file. Attempts to change append, col.names, sep, dec or qmethod are ignored, with a warning.

So it not only defaults to this seperator, it enforces it. Use

write.table(data,file="data.csv",sep=",",dec = " ")
Maria answered 26/4, 2017 at 20:52 Comment(0)
H
1

You can also use fwrite, fast CSV writer, with option sep = ";".

Hordein answered 5/5, 2018 at 9:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.