使用cat编写data.frame

时间:2014-08-26 16:59:51

标签: r dataframe cat

如何将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)

2 个答案:

答案 0 :(得分:3)

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

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

答案 1 :(得分:2)

试试这个

capture.output(abc, file = fileConn)