readr::read_*()
get_help()
docs
All functions documented here are part of the {readr}
package, which is part of the {tidyverse}
.
The {readr}
package provides several useful functions for reading delimited data files into R. Delimited data files have rows and columns, where columns are separated aka delimited by a certain character, such as a comma ,
or tab symbol.
When using these functions, your data automatically gets read in as a tibble. This is usually preferable to using base R functions like read.csv()
or read.table()
, which create data frames.
These functions include:
Function | What kinds of files it reads |
---|---|
read_csv() |
CSV (comma separated values) files |
read_tsv() |
TSV (tab separated values) files |
read_csv2() |
Files with a semi-colon ; delimiter. (This format is commonly used in certain European countries which use commas instead of periods in their decimals) |
read_delim() |
Files with any delimiter symbol, which you specify with the argument delim |
To use these function, you need to either first load the {readr}
library, or always use the function with readr::
notation.
# Load the library
library(readr)
# Or, load the full tidyverse:
library(tidyverse)
# Or, use :: notation
::read_csv()
readr::read_tsv()
readr::read_csv2()
readr::read_delim() readr
read_csv("path/to/file/to/read/in/file.csv")
read_tsv("path/to/file/to/read/in/file.tsv")
read_csv2("path/to/file/to/read/in/file.csv")
read_delim("path/to/file/to/read/in/file.csv", delim = ",") # for example, a comma delimiter
# Read in (hypothetical) data file called "data.csv" which lives in the _same directory_ as your code,
# and save to a variable `my_data`
<- read_csv("data.csv") my_data
# Read in (hypothetical) data file called "data.tsv" which lives in the relative directory "datasets/"
# and save to a variable `my_data`
<- read_tsv("dataset/data.tsv") my_data
# Read in (hypothetical) data file called "data_weird.txt" which lives in the relative directory "datasets/"
# and uses the weird approach of exclamation marks `!` to delimit (separate) columns
<- read_delim("datasets/data_weird.txt", delim = "!") my_data
# CAUTION! If you have a typo or an incorrectly specified path to the data, you will get an error saying:
# Your file 'does not exist in current working directory'
<- read_csv("this_file_doesnt_exist.csv") my_data
## Error: 'this_file_doesnt_exist.csv' does not exist in current working directory ('/Users/spielman/Projects/introverse/inst/rmd_topics').