我有一个问题,将列表写入r中的文本文件.这是我的代码:
library(e1071)
mydata = read.table("TRAIN.txt", sep = ",", header = FALSE)
model <- naiveBayes(as.factor(V1) ~., data = my data)
Run Code Online (Sandbox Code Playgroud)
我想把"模型"写入文本文件.这是"模型"格式:
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
Run Code Online (Sandbox Code Playgroud)
我尝试了以下方法:
write(model, "TEST.txt")
Run Code Online (Sandbox Code Playgroud)
并得到以下错误:
Error in cat(list(...), file, sep, fill, labels, append) :
argument 1 (type 'list') cannot be handled by 'cat'
Run Code Online (Sandbox Code Playgroud)
然后我试了
lapply(model, cat, file='test.txt', append=TRUE)
Run Code Online (Sandbox Code Playgroud)
并得到了同样的错误.
y1 <- rnorm(100)
x1 <- rnorm(100)
model.out <- lm(y1~x1)
sink("~/Desktop/TEST.txt", type=c("output", "message"))
model.out
sink(NULL)
Run Code Online (Sandbox Code Playgroud)
基于这个答案:如何将所有控制台输出保存到R中的文件?