zoukankan      html  css  js  c++  java
  • R语言的输出函数cat,sink,writeLines,write.table

    R语言的输出函数cat,sink,writeLines,write.table

    根据输出的方向分为输出到屏幕和输出到文件。

    1.cat函数即能输出到屏幕,也能输出到文件.

    使用方式:cat(... , file = "", sep = " ", fill = FALSE, labels = NULL,append = FALSE)

    有file时,输出到file。无file时,输出到屏幕。

    append参数:布尔值。TRUE时,输出内容追加到文件尾部。FALSE,覆盖文件原始内容。

    1
    2
    3
    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,覆盖文件原始内容。

    1
    2
    3
    sink("hello.txt") # 将输出重定向到hello.txt
    cat("hello")
    sink() # 结束重定向

     

    3.writeLines函数将字符串向量输出到文件中(会覆盖原始内容)

    使用方式:writeLines(text, con = stdout(), sep = " ", useBytes = FALSE)

    text:字符串向量;con:输出文件

     a=c("one","tew")
     writeLines(a,con="D:/test.txt",sep="	")

    问题:每调用一次就会覆盖原始内容?

    4.write.table()函数将dataframe的内容输出到文件。

    使用方式:write.table(x, file = "", append = FALSE, quote = TRUE, sep = " ",eol = " ", na = "NA", dec = ".", row.names = TRUE,col.names = TRUE, qmethod = c("escape", "double"),fileEncoding = "")

    1
    2
    3
    m=matrix(1:12,nrow=3)
    df=as.data.frame(m)
    write.table(df,file="D:/test.txt",append=T,row.names=F)
  • 相关阅读:
    linux指令备份
    jdk安装
    java-成员变量的属性与成员函数的覆盖
    Codeforces Round #384 (Div. 2) E
    Codeforces Round #384 (Div. 2) ABCD
    Codeforces Round #383 (Div. 2) D 分组背包
    ccpcfinal总结
    HDU 3966 & POJ 3237 & HYSBZ 2243 & HRBUST 2064 树链剖分
    HDU 5965 枚举模拟 + dp(?)
    Educational Codeforces Round 6 E dfs序+线段树
  • 原文地址:https://www.cnblogs.com/caizhao/p/9328241.html
Copyright © 2011-2022 走看看