[R] How to export text into separate text files

Lauri Nikkinen lauri.nikkinen at iki.fi
Tue Oct 28 15:26:38 CET 2008


Hello,

I'm producing text from my data.frame using cat function. I would like
to use for loop to export each column in my data.frame into separate
text files. Here is the example code

r <- t(Indometh)
for (i in 1:ncol(r)) {
     cat("Some text,", "\n")
     cat("\n")
     cat("More text, More text, More text")
     cat("\n")
     cat(paste(names(r[!is.na(r[,i]), i]), ":",
as.character(r[!is.na(r[,i]), i]), "\n"))
     cat("-----------------------------------", "\n")
}

So, each of these columns (results between --- and ---) should be
exported into separate text files e.g. file1.txt, file2.txt, file3.txt
etc.

Any ideas?

Thanks,
Lauri



More information about the R-help mailing list