• 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 = " ", 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 = "")

    m=matrix(1:12,nrow=3)
    df=as.data.frame(m)
    write.table(df,file="D:/test.txt",append=T,row.names=F)
    

      

     

    拒绝低效率勤奋,保持高效思考
  • 相关阅读:
    chrome.declarativeWebRequest
    webRequest模块的解读
    C#连接池
    sftp
    Lynx
    LD_PRELOAD & LD_LIBRARY_PATH 动态库路径
    libc.so.6 误删后修复
    man 转 pdf _____ jpg 转 pdf
    here文档
    lsof fuser
  • 原文地址:https://www.cnblogs.com/timeisbiggestboss/p/7766115.html
Copyright © 2020-2023  润新知