Read/Write Data into ‘R’ Language

Read

There are few basic functions available to read data into R.

  • read.table, read.csv - To read tabular data. These functions are most commonly used function to read the data.
  • readLine – To read the text files as each lines. This function can read any type of text files and will return a list of lines.
  • source – It’s used to read the ‘R’ code. For example, if we want to read a R function from the file the ‘source’ function will be used to achieve it.
  • dget – This is also used to read the ‘R’ code but actually the R objects that are deparsed in to text files.
  • load, unserialize – These functions are used to read the binary object into R.

But there are different R packages developed to read different kind of other dataset.

Mostly in these functions we need to set few arguments to read the data effectively and it will increase the performance while dealing with large datasets.

read.table(file, header = FALSE, sep = "", quote = "\"'",
           dec = ".", numerals = c("allow.loss", "warn.loss", "no.loss"),
           row.names, col.names, as.is = !stringsAsFactors,
           na.strings = "NA", colClasses = NA, nrows = -1,
           skip = 0, check.names = TRUE, fill = !blank.lines.skip,
           strip.white = FALSE, blank.lines.skip = TRUE,
           comment.char = "#",
           allowEscapes = FALSE, flush = FALSE,
           stringsAsFactors = default.stringsAsFactors(),
           fileEncoding = "", encoding = "unknown", text, skipNul = FALSE)

read.csv(file, header = TRUE, sep = ",", quote = "\"",
         dec = ".", fill = TRUE, comment.char = "", ...)

read.csv2(file, header = TRUE, sep = ";", quote = "\"",
          dec = ",", fill = TRUE, comment.char = "", ...)

read.delim(file, header = TRUE, sep = "\t", quote = "\"",
           dec = ".", fill = TRUE, comment.char = "", ...)

read.delim2(file, header = TRUE, sep = "\t", quote = "\"",
            dec = ",", fill = TRUE, comment.char = "", ...)

Write

  • write.table – To write tabular structure data to text files.
  • writeLines – To write data/set of characters in line by line to files.
  • dump – To dump a textual representation of multiple R objects.
  • dput – To output a textual representation of an R object.
  • save – To save objects (arbitrary number) into binary format.
  • serialize - To convert objects into binary format.
write.table(x, file = "", append = FALSE, quote = TRUE, sep = " ",
            eol = "\n", na = "NA", dec = ".", row.names = TRUE,
            col.names = TRUE, qmethod = c("escape", "double"),
            fileEncoding = "")

write.csv(...)
write.csv2(...)

The read/write parameters are mostly straightforward. But if you need more details of all the parameters for the functions, You can find the R manuals in this Link.

Blog Series