【问题标题】:writing a data.frame using cat使用 cat 编写 data.frame
【发布时间】:2014-10-20 02:25:46
【问题描述】:

如何将 data.frame abc 添加/附加到我之前打开的文本文件中。我正在向该文件写入一些重要信息,然后我想将该 data.frame 附加到该信息下方。当我尝试使用 cat 编写 data.frame abc 时出现错误。

fileConn<-file("metadata.txt","w+")
smoke <- matrix(c(51,43,22,92,28,21,68,22,9),ncol=3,byrow=TRUE)
smoke <- as.data.frame(smoke)
table <- sapply (smoke, class)
abc <- data.frame(nm = names(smoke), cl = sapply(unname(smoke), class))
cat("some imp info","\n", file=fileConn)
cat(abc,"\n", file=fileConn)
close(fileConn)
class(abc)

【问题讨论】:

  • capture.output(abc, file = fileConn) 也可以
  • @lukeA 它有效。请添加它作为答案,我会接受它

标签: r dataframe cat


【解决方案1】:

试试这个

capture.output(abc, file = fileConn)

【讨论】:

    【解决方案2】:

    只需使用标准工具编写data.frame,即write.table

    write.table(abc, 'yourfile', append=TRUE) # plus whatever additional params
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-07-29
      • 1970-01-01
      • 1970-01-01
      • 2012-12-29
      • 1970-01-01
      • 2021-11-03
      • 2015-04-17
      • 2012-07-11
      相关资源
      最近更新 更多