【R】R语言结果输出方法

输出函数: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)
posted @ 2018-10-08 17:09  望着小月亮  阅读(24436)  评论(0编辑  收藏  举报