R中write.table()函数的不需要的输出

时间:2013-11-06 07:31:38

标签: r csv

我有一个csv文件,我使用read.csv加载到R中并执行了一些操作。而且,现在我需要将其写回csv格式。但问题是,write.table()函数不会产生require输出。

初始数据:

id,name,Households,Population,Total Male,Total Female
0,Nepal,5427302,26494504,12849041,13645463
1,Taplejung,26509,127461,60552,66909
2,Panchthar,41196,191817,90186,101631
3,Ilam,64502,290254,141126,149128
4,Jhapa,184552,812650,385096,427554

使用write.table()

的最终输出
"id","name","Households","Population","SexRatio"
"1",0,"Nepal",5427302,26494504,94.1634666408901
"2",1,"Taplejung",26509,127461,90.499036004125
"3",2,"Panchthar",41196,191817,88.7386722555126
"4",3,"Ilam",64502,290254,94.634139799367
"5",4,"Jhapa",184552,812650,90.0695584651295

如何以输入格式获得最终输出?

1 个答案:

答案 0 :(得分:2)

我猜你的主要抱怨是关于输出附加的“行名称”以及所有字符串周围的引号。

如果是这种情况,请查看row.names中的参数quotewrite.table()(以及write.csv()),并在FALSE时将其设置为inData你正在写你的文件。

以下是一个示例,假设您的R中的数据称为inData

首先,我们将write.csv写入一个文件,其默认参数为write.csv(inData, file = "oldfile.csv") cat(readLines("oldfile.csv"), sep="\n") # "","id","name","Households","Population","Total.Male","Total.Female" # "1",0,"Nepal",5427302,26494504,12849041,13645463 # "2",1,"Taplejung",26509,127461,60552,66909 # "3",2,"Panchthar",41196,191817,90186,101631 # "4",3,"Ilam",64502,290254,141126,149128 # "5",4,"Jhapa",184552,812650,385096,427554 。请注意行名称和引用的字符串。

inData

使用我提到的FALSE设置的参数写write.csv(inData, file = "newfile.csv", row.names = FALSE, quote = FALSE) cat(readLines("newfile.csv"), sep="\n") # id,name,Households,Population,Total.Male,Total.Female # 0,Nepal,5427302,26494504,12849041,13645463 # 1,Taplejung,26509,127461,60552,66909 # 2,Panchthar,41196,191817,90186,101631 # 3,Ilam,64502,290254,141126,149128 # 4,Jhapa,184552,812650,385096,427554 时也是如此:

{{1}}