5

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

and I tried the following:

write(model, "TEST.txt")

and got the following error:

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

and then I tried

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

and got the same error.

rbatt
  • 4,677
  • 4
  • 23
  • 41
MTT
  • 5,113
  • 7
  • 35
  • 61
  • Do you want to be able to reload the model from the text file? Or just write the same output you see in the console to a file? – MrFlick Apr 30 '15 at 01:18
  • Just write the same output I see in the console. I do not want to retrieve it later. – MTT Apr 30 '15 at 01:21

1 Answers1

7
y1 <- rnorm(100)
x1 <- rnorm(100)
model.out <- lm(y1~x1)

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

Based on this answer: How to save all console output to file in R?

Community
  • 1
  • 1
rbatt
  • 4,677
  • 4
  • 23
  • 41