【发布时间】:2016-07-28 05:10:39
【问题描述】:
我有 2 个矩阵 x 和 y。我想按以下顺序将它们写在同一个 csv 文件中:矩阵 x、“一些文本”、矩阵 y。我使用函数 write.table 来附加文件。但是,只有 y 矩阵被写入而 x 矩阵丢失。我注意到,如果我在 2 个命令 write.table 中删除“一些文本”,两个矩阵都会正确打印出来。实际上,我想在打印矩阵 y 之前保留“一些文本”,因为这将帮助我识别我在大数据测试中的结果。你有什么建议吗?谢谢。
rm(list=ls())
cat("\014")
file_ext="csv"
output_file = paste("result", file_ext, sep = ".")
(x <- matrix(1, nrow = 3, ncol = 2, byrow = TRUE))
(y <- matrix(2, nrow = 3, ncol = 2, byrow = TRUE))
sink(output_file)
#------------------------------------------------------------
cat("The results are:",sep="\n")
#write the x matrix. PROBLEM: the x matrix is not printed out
cat("The x matrix",append =TRUE)
cat("\n")
write.table(x, file=output_file, sep=",", col.names = F, row.names = F, append = TRUE)
#write the y matrix.
cat("The y matrix",append =TRUE)
cat("\n")
write.table(y, file=output_file, sep=",", col.names = F, row.names = F, append = TRUE)
sink()
file.show(output_file) #show the file in directory
【问题讨论】:
标签: r