輸出函數:cat,sink,writeLines,write.table
根據輸出的方向分為輸出到屏幕和輸出到文件。
1.cat函數即能輸出到屏幕,也能輸出到文件.
使用方式:cat(... , file = "", sep = " ", fill = FALSE, labels = NULL,append = FALSE)
有file時,輸出到file。無file時,輸出到屏幕。
append參數:布爾值。TRUE時,輸出內容追加到文件尾部。FALSE,覆蓋文件原始內容。
cat("hello") hello cat("hello",file="D:/test.txt",append=T)
2.sink函數將輸出結果重定向到文件。
使用方式:sink(file = NULL, append = FALSE, type = c("output", "message"),split = FALSE)
append參數:布爾值。TRUE時,輸出內容追加到文件尾部。FALSE,覆蓋文件原始內容。
sink("hello.txt") # 將輸出重定向到hello.txt cat("hello") sink() # 結束重定向
3.writeLines函數將字符串向量輸出到文件中(會覆蓋原始內容)
使用方式:writeLines(text, con = stdout(), sep = "\n", useBytes = FALSE)
text:字符串向量;con:輸出文件
a=c("one","tew") writeLines(a,con="D:/test.txt",sep="\t")
問題:每調用一次就會覆蓋原始內容?
4.write.table()函數將dataframe的內容輸出到文件。
使用方式:write.table(x, file = "", append = FALSE, quote = TRUE, sep = " ",eol = "\n", na = "NA", dec = ".", row.names = TRUE,col.names = TRUE, qmethod = c("escape", "double"),fileEncoding = "")
m=matrix(1:12,nrow=3) df=as.data.frame(m) write.table(df,file="D:/test.txt",append=T,row.names=F)