Base R Essentials - Part 2

Some essentials Base R functions still important to know in the Tidyverse ecosystem (Read/Write file)

R
Base
Author

Abdoul ISSA BIDA

Published

February 17, 2023

This second blog post of the series focuses on reading and writing files of different formats. Although most of the functions focus on reading and writing tabular data, there are also functions to extend the principles to plain text.

Functions Tasks / Examples
load("filepath") reloads dataset written with the function save
data(x) loads the dataset x
readLines(con, n) read some some or all text lines from connection, where con is supposed to be a connection object, but when it is a string, it is considered as a file path. n is the maximal number of lines to read
read.table("filepath") reads tabular data transforming it to a data frame. There are multiple arguments:
- sep: to specify the separator
- header: a boolean (TRUE or FALSE) to take the first row or not
- skip: an integer n to indicate the number of rows to skip
Ex: read.table("weight.txt", sep = ",", header = T, skip = 5)
read.csv("filepath") same that read.table except that there are predefined arguments values to read CSV (Comma Separated Values) files
read.csv2("filepath") same as read.csv except the dec1 argument default value is , here and . in case of read.csv
read.delim("filepath") same as read.table except that there are predefined arguments values to read values separated by tabulations
read.fwf("filepath", widths) reads a table of fixed width formatted2 data a into data frame. The widths argument is an integer vector, giving the widths of the fixed-width fields.
save( x, y, file = "file.RData")3 writes an external representation of objects x and y to the specified file. The objects can be read back from the file at a later date by using the function load.
save.image() or save.image(file = "file.RData") same as save except that it saves all the environment objects in .RData file if file argument is not specified, else in file.
cat(..., file = "filepath") outputs the objects, concatenating the representations in the file specified.
Ex: cat(x, y, file = "xy.txt") converts x and y objects to characters and writes that representation in the file xy.txt.
print(x, ... ) prints the object x. The printing format varies according to the argument class.
format(x, ...) format x for pretty printing. There are several options to fit your need.
Ex: format(13.7, nsmall = 3) to indicate the minimum number of digits to the right of the decimal point. Feel free to look at the others options help(format).
writeLines(text, con) writes the the character vector text in the connection object con of the file path given by con if it is a string
write.table(x, file = "filepath") convert x to a data frame if it is not and writes it to the file specified. In the same way, there are various other functions(write.csv, write.csv2 and many others) for different outputs.

Footnotes

  1. the character used in the file for decimal points↩︎

  2. The format is designed to allow for saving of textual information/data in an organized fashion.↩︎

  3. .RData is a binary file format specific to R.↩︎

Citation

BibTeX citation:
@online{issabida2023,
  author = {Abdoul ISSA BIDA},
  title = {Base {R} {Essentials} - {Part} 2},
  date = {2023-02-17},
  url = {https://www.abdoulblog.com},
  langid = {en}
}
For attribution, please cite this work as:
Abdoul ISSA BIDA. 2023. “Base R Essentials - Part 2.” February 17, 2023. https://www.abdoulblog.com.