read_delim("my/file.txt")Read a character-delimited file (.txt)
You want to read a delimited data file into your R session, where you can manipulate its contents. The file typically has the file extension .txt. Within each row of a delimited file, the column fields are separated by a character delimiter. Some common choices for the delimiter are |, :, ;, ,, or ~.
Step 1 - Call readr::read_delim().
Step 2 - Give read_delim() the filepath to your dataset as a character string. For example:
Step 3 - Specify the delim argument to tell read_delim() the delimiter character.
read_delim("my/file.txt", delim = "|")Step 4 - Save the output to an object, so you can access it later.
delim_table <- read_delim("my/file.txt", delim = "|")Example
We want to read in the solar dataset which is a delimited dataset .txt file and uses the delimiting character |. This dataset contains weekly solar power production records from power plants in Alabama. We have solar saved on our computer at /Users/bcullen/Desktop/rstudio/academySassyRecipes/recipes/basics/data/solar.txt.
We begin by loading the readr package which contains read_delim(). Then, we pass read_delim() the filepath for our .txt file in order to read in the dataset. The file is in the data folder in our working directory. The output looks like this:
library(readr)
read_delim("data/solar.txt", delim = "|")Rows: 548 Columns: 3
── Column specification ────────────────────────────────────────────────────────
Delimiter: "|"
chr (1): plant
dbl (1): power
date (1): date
ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
# A tibble: 548 × 3
plant date power
<chr> <date> <dbl>
1 PV1 2006-06-04 19731.
2 PV1 2006-06-11 15854.
3 PV1 2006-06-18 18508.
4 PV1 2006-06-25 16711.
5 PV2 2006-06-04 7995.
6 PV2 2006-06-11 7137.
7 PV2 2006-06-18 6772.
8 PV2 2006-06-25 7512.
9 PV3 2006-06-04 8599.
10 PV3 2006-06-11 7627.
# ℹ 538 more rows
Notice that read_delim() automatically chose intelligent data types for each of the columns.
Lastly, we assign the solar dataset read in by read_delim() to an object, solar, so we can access it later.
solar <-read_delim("data/solar.txt", delim = "|")Rows: 548 Columns: 3
── Column specification ────────────────────────────────────────────────────────
Delimiter: "|"
chr (1): plant
dbl (1): power
date (1): date
ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
read_delim() is a more general case of readr::read_csv() and readr::read_tsv()
read_csv() is the equivalent of calling read_delim() with delim = ",". read_tsv() is the equivalent of read_delim() with delim - "\t".
read_delim() and SAS
read_delim() is the equivalent of SAS’s IMPORT procedure with the DBMS = DLM and DELIMITER = 'character' options. In SAS, you must specify the complete path, filename, and extension for DATAFILE, whereas when using read_delim() the path can be specified relative to the working directory.
IN SAS:
PROC IMPORT DATAFILE = 'C:/Users/my/file.txt' OUT = delim_table
DBMS = DLM REPLACE DELIMITER = 'character';In R:
delim_table <- read_delim("my/file.txt", delim = "character")