Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

set separator ';' in write.csv

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?

like image 690
dsent Avatar asked Jun 23 '16 19:06

dsent


People also ask

How do I change the separator in a CSV file?

Change separator when saving Excel file as CSVClick File > Options > Advanced. Under Editing options, clear the Use system separators check box. Change the default Decimal separator. As this will change the way decimal numbers are displayed in your worksheets, choose a different Thousands separator to avoid confusion.

What is the default separator and decimal character used by write CSV?

csv() uses “.” for the decimal point and a comma (“,”) for the separator.


2 Answers

First, you should provide a reproducible example.

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

like image 147
Michael Kirchner Avatar answered Jan 29 '23 03:01

Michael Kirchner


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 = " ")
like image 26
ynux Avatar answered Jan 29 '23 03:01

ynux