【问题标题】:Write a list, as seen in R console output, into a text file将 R 控制台输出中看到的列表写入文本文件
【发布时间】:2015-04-30 01:07:11
【问题描述】:

我在将列表写入 r 中的文本文件时遇到问题。这是我的代码:

library(e1071)
mydata = read.table("TRAIN.txt", sep = ",", header = FALSE)
model <- naiveBayes(as.factor(V1) ~., data = my data)

我想将“模型”写入文本文件。这是“模型”格式:

A-priori probabilities:
Y
   0        1 
0.703125 0.296875 

Conditional probabilities:
V2
Y         [,1]      [,2]
0  0.1327792 1.1571522
1 -0.1276267 0.9334735

V3
Y         [,1]      [,2]
0 -0.2414282 1.0982461
1 -0.2269481 0.7594525

我尝试了以下方法:

write(model, "TEST.txt")

并得到以下错误:

Error in cat(list(...), file, sep, fill, labels, append) : 
argument 1 (type 'list') cannot be handled by 'cat'

然后我尝试了

lapply(model, cat, file='test.txt', append=TRUE)

并得到同样的错误。

【问题讨论】:

  • 您希望能够从文本文件中重新加载模型吗?或者只是将您在控制台中看到的相同输出写入文件?
  • 只需编写我在控制台中看到的相同输出即可。我不想稍后再检索它。

标签: r cat lapply


【解决方案1】:
y1 <- rnorm(100)
x1 <- rnorm(100)
model.out <- lm(y1~x1)

sink("~/Desktop/TEST.txt", type=c("output", "message"))
model.out
sink(NULL)

基于此答案:How to save all console output to file in R?

【讨论】:

    猜你喜欢
    • 2012-02-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-18
    • 2014-03-08
    • 2011-03-17
    • 1970-01-01
    相关资源
    最近更新 更多