R/Files

From QERM Wiki
Revision as of 20:42, 2 June 2010 by Ian (Talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Contents

Reading files

Writing files

There are several ways to write files in R

cat() and sink()

Example:

sink("C://filepath//filename.txt", append=FALSE, split=FALSE)

VAR1=3.5; VAR2=5

cat(paste(VAR1, "x+", VAR2, "y<j", sep=""))

sink()

Tips:

  • Special characters
    • \n newline
    • \t tab
    • \" quote
  • Use this technique along with xtable() to easily write pretty TeX files for tables

write() and friends

  • write() will write data to the file specified with the file argument
  • write.table() for writing matrices/data frames (use append=T if already text in document)
  • write.csv() for writing comma-separated files
  • file() / close() to open and close connections to file for writing or reading
  • writeLines() / readLines() for writing lines and reading lines of a file ( readLines() is especially helpful for getting files into R that can't be handled by scan() or read.table() )
Personal tools