简体   繁体   English

在R中使用write.table写入csv文件时出现不良对齐

[英]Undesirable alignment when writing csv file using write.table in R

I am trying to write and append my output to a csv file using R . 我正在尝试使用R编写输出并将其附加到csv文件。 First, the program will write the header and then inside a for loop append the generated rows. 首先,程序将写入标头,然后在for循环内附加生成的行。 An example code is as follows: 示例代码如下:

   writeLines(text=c('Mean','Sd','\n'),'file.csv',sep=',')
   for(i in 1:5)
    {x <- rnorm(10); dat1 <- cbind(mean(x),sd(x))
      write.table(dat1,'file.csv',append=TRUE,sep=',',col.names=FALSE)
    }

But this shifts my first row to right by one element giving the following in csv output. 但这会将我的第一行向右移动一个元素,从而在csv输出中提供以下内容。 How can I avoid this? 如何避免这种情况? Is there another way to achieve what I am trying to do here? 还有另一种方法可以实现我在这里要做的事情吗?

EDIT Also, how do we remove the column labeling which is 1 in this case? 编辑另外,在这种情况下,如何删除列标记为1的列? col.names=FALSE does not seem to help. col.names=FALSE似乎没有帮助。 I used row.names=FALSE as suggested by @Roman Luštrik. 我使用了row.names=FALSE所建议的row.names row.names=FALSE It solved this problem. 它解决了这个问题。 But the first row shifting problem is not yet to be addressed. 但是,第一行移位问题尚未解决。

在此处输入图片说明

I've often had to write to files in such a manner. 我经常不得不以这种方式写入文件。 I usually do it this way. 我通常这样做。

If file already exists, just append without the column and row names. 如果文件已经存在,只需追加不包含列名和行名的文件。 If not, create a brand new one. 如果没有,请创建一个全新的。

x <- rnorm(10); dat1 <- data.frame(Mean = mean(x), Sd = sd(x))
if (file.exists("file.csv")) {
   write.table(dat1, file = "file.csv", append = TRUE, col.names = FALSE, row.names = FALSE, sep = ",")
} else {
   write.table(dat1, file = "file.csv", col.names = TRUE, row.names = FALSE, sep = ",")
}

Why not use paste() ? 为什么不使用paste()呢?

writeLines(text=paste(c('Mean','Sd'),collapse=','),'file.csv',sep="\n")
for(i in 1:5) {
  x <- rnorm(10); dat1 <- cbind(mean(x),sd(x))
  write.table(dat1, 'file.csv', append=TRUE, sep=',', row.names=FALSE, col.names=FALSE)
}

To understand how the CSV file looks like, I suggest that you open it in a text editor and not in Excel (which transforms it). 要了解CSV文件的外观,建议您在文本编辑器中而不是在Excel中(将其转换)打开它。 Because then you could see that your CSV starts like: 因为这样您可以看到您的CSV像这样开始:

Mean,Sd,
,"1",0.441737369447746,1.07695222886305
[...]

...two too many commas. ...两个逗号过多。

In your writeLines statement, what you meant is the following: 在您的writeLines语句中,您的意思是:

   writeLines(paste('Mean','Sd',sep=","),'file.csv')

What is wrong in your case is that you replaced the default sep="\\n" of writeLines with sep="," and apparently writeLines prints a separator also after the last item, unlike paste . 您遇到的问题是,您用sep=","替换了默认的writeLines sep="\\n" sep=","并且显然writeLines在最后一项之后也打印了一个分隔符,这与paste不同。 (This makes much more sense when printing lines as the default operation, closing also the last line with a \\n .) (这在将行作为默认操作打印时更有意义,因为最后一行也用\\n 。)

In write.Table , use row.names=FALSE . write.Table ,使用row.names=FALSE

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM