Skip to content
Prev 43653 / 398513 Next

Novice problems with write()

What you have done is write 6 through 15 to the file test.txt 10 times, each
with one number.  write() opens a file, write to it, and then close the
file, so if you do it in the loop, it would open the file 10 times, write
one number to it 10 times, and close the file 10 times.

You're probably looking for something like:

fout <- file("c:/test.txt", "w")
x <- 5
for (i in 1:10) {
  z <- x + i
  writeLines(paste(z, "\n"), fout)
}
close(fout)

Or simply:

write(5 + 1:10, file="c:/test.txt", ncol=1)

Andy
------------------------------------------------------------------------------
Notice:  This e-mail message, together with any attachments,...{{dropped}}