将列表(如R控制台输出所示)写入文本文件 [英] Write a list, as seen in R console output, into a text file
本文介绍了将列表(如R控制台输出所示)写入文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在将列表写入r中的文本文件时遇到问题.这是我的代码:
I have problem with writing a list into a text file in r. Here is my code:
library(e1071)
mydata = read.table("TRAIN.txt", sep = ",", header = FALSE)
model <- naiveBayes(as.factor(V1) ~., data = my data)
,我想将模型"写入文本文件.这是模型"格式:
and I want to write the "model" into a text file. Here is the "model" format:
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)
,并得到相同的错误.
and got the same error.
推荐答案
y1 <- rnorm(100)
x1 <- rnorm(100)
model.out <- lm(y1~x1)
sink("~/Desktop/TEST.txt", type=c("output", "message"))
model.out
sink(NULL)
基于此答案:如何保存所有内容控制台输出到R中的文件?
这篇关于将列表(如R控制台输出所示)写入文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文