2
我有2個矩陣x和y。我想按照以下順序將它們寫在同一個csv文件中:矩陣x,「一些文本」,矩陣y。我使用函數write.table來追加文件。但是,只有在x矩陣丟失時寫入y矩陣。我注意到,如果我在兩個命令write.table中刪除「一些文本」,兩個矩陣都將被正確打印出來。實際上,我想在打印矩陣之前保留「一些文本」,因爲這將幫助我在大數據測試中識別我的結果。你有什麼建議嗎?謝謝。R write.table不會在同一個文件上寫入2個矩陣
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
是的,這是我預期的結果。你的修復工作完美。非常感謝你。 – user6603604