设置分隔符 ';'在 write.csv

set separator ';' in write.csv

我有以下代码:

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

很遗憾,分隔符仍然是 ',':

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

运行 代码给我:

警告消息:

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 = ";",  :

尝试设置 'sep' 被忽略

我做错了什么?

首先,您应该提供一个可重现的示例。

但是,如果您使用 write.csv2,它默认使用分号作为分隔符。

文档 https://stat.ethz.ch/R-manual/R-devel/library/utils/html/write.table.html 说:

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.

所以它不仅默认使用这个分隔符,而且强制执行它。使用

write.table(data,file="data.csv",sep=",",dec = " ")

您还可以使用 fwritefast CSV writer 和选项 sep = ";"