在 R 中写入 csv 文件时的分隔符

Delimiters while writing csv files in R

在 R 中编写 csv 文件时如何使用 |(管道)作为分隔符?

当我尝试使用 write.csvsep = "|" 将数据集写入文件时,它会忽略分隔符并将文件简单地写为逗号分隔文件。

此外 write.csv2 似乎也没有涵盖可用作分隔符的其他各种字符。

在 R 中写入 csv 文件时,是否可以使用其他字符(例如 ^、$、~、¬ 或 |)作为分隔符。

谢谢。

你要明白.csv的意思就是"comma-separated value"https://en.wikipedia.org/wiki/Comma-separated_values.

如果您想要使用该字符使用分隔符导出您需要另一个功能。

例如,使用 write.table,您将能够使用 RExcel、....

加载此文件
write.table(data, "data.txt", sep = "|")
data_load <- read.table("data.txt", sep = "|")

随意使用任何字符作为分隔符。

或者您可以将此纯文本强制为 .csv

write.table(data, "data.csv", sep = "|")
data_load <- read.csv("data.csv", sep = "|")

这个答案只是我为此 给出的答案的变体。它们很相似,但我不认为问题本身是完全重复的,但它们都是更大问题(尚未提出)的一部分。

write.table 的帮助中指出:

write.csv and write.csv2 provide convenience wrappers for writing CSV files.

...

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.

要设置 sep 或其他参数,您需要使用 write.table 而不是 write.csv