The base R provides essential functions for importing data:
read.table()
—the most general function of the base R for importing data, takes in tabular data with any kind of field separators, including specific ones, such as |.read.csv()
—comma-separated values (CSV) files with.
as the decimal separator.read.csv2()
—semicolon-separated values files with,
as the decimal separator.read.delim()
—tab-separated values (TSV) files with.
as the decimal separator.read.delim2()
—tab-separated values (TSV) files with,
as the decimal separator.
In practice, any of these functions can be used to import tabular data with any kind of field and decimal separators: using them for the specified formats of files is only the question of convention and default settings. For example, here is the syntax of the first function: read.table(file, header = FALSE, sep = "", dec = ".")
. The other functions have the same parameters with different default settings that can always be explicitly overwritten.
The tidyverse packages readr and readxl provide some other functions for importing specific file formats. Each of those functions can be further fine-tuned by setting various optional parameters.
readr
read_tsv()
—tab-separated values (TSV) files.read_fwf()
—fixed-width files.read_log()
—web log files.read_table()
,read_csv()
,read_csv2()
, andread_delim()
—equivalent to the base R functions.
readxl
read_excel()
—Excel files.read_csv()
—equivalent to the function from the base R functions.
To dive deeper into data loading in R, you can go through the tutorial on How to Import Data Into R.
Leave a Reply