[英]Delimiters while writing csv files in R
How can I use |
我如何使用
|
(pipe) as a delimiter while writing csv files in R? (管道)作为分隔符,同时在R中写入CSV文件?
When I try writing a data set into a file with write.csv
with sep = "|"
当我尝试使用带有
sep = "|"
write.csv
将数据集写入文件时 , it ignores the separator and writes the file simply as a comma separated file. ,它将忽略分隔符,并将文件简单地以逗号分隔的文件形式写入。
Also write.csv2
also doesn't seem to cover the other variety of characters which could be used as a separator. 同样,
write.csv2
似乎也没有涵盖可用作分隔符的其他各种字符。
Is there a way to use other characters such as ^, $, ~, ¬ or |, as a delimiter while writing a csv file in R. 在用R编写csv文件时,是否可以使用其他字符(例如^,$,〜,¬或|)作为定界符。
Thanks. 谢谢。
You have to understand that .csv
means "comma-separated value" https://en.wikipedia.org/wiki/Comma-separated_values . 您必须了解
.csv
意思是“逗号分隔的值” https://en.wikipedia.org/wiki/Comma-separated_values 。
If you want to export with a separator using that characters you need another function. 如果要使用使用该字符的分隔符导出,则需要另一个功能。
For example, using write.table
, and you'll be able to load this file with R
, Excel
,.... 例如,使用
write.table
,您将可以使用R
, Excel
.....加载此文件。
write.table(data, "data.txt", sep = "|")
data_load <- read.table("data.txt", sep = "|")
Feel free to use any character as separator. 随意使用任何字符作为分隔符。
Or you could force this plain text to be .csv
或者,您可以强制将此纯文本
.csv
为.csv
write.table(data, "data.csv", sep = "|")
data_load <- read.csv("data.csv", sep = "|")
This answer is just a variation of the one I gave for this question . 这个答案只是我对此问题给出的答案的一种变体。 They are similar, but I don't think the question itself is an exact duplicate, but they are both part of a bigger question (not yet asked).
它们是相似的,但我不认为问题本身是重复的,但它们都是较大问题(尚未提出)的一部分。
In the help for write.table
, it states: 在
write.table
的帮助中,它指出:
write.csv and write.csv2 provide convenience wrappers for writing CSV files.
write.csv和write.csv2提供了用于编写CSV文件的便捷包装。
...
...
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,dec或qmethod的尝试将被忽略,并显示警告。
To set sep
or another of these parameters you need to use write.table
instead of write.csv
. 要设置
sep
或这些参数中的另一个,您需要使用write.table
而不是write.csv
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.