| read_dir | R Documentation |
Reads data files from any given directory as data frames and
merges them into a single data frame (using
data.table::rbindlist).
read_dir(
pattern = "*[.]",
path = ".",
reader_function = data.table::fread,
...,
subdirs = FALSE,
filt = NULL,
hush = FALSE
)
pattern |
Regular expression ("regex"; as string or |
path |
Path to the directory from which the files should be selected and
read. The default |
reader_function |
A function to be used for reading the files,
|
... |
Any arguments to be passed on to the chosen |
subdirs |
Logical ( |
filt |
An expression to filter, by column values, each data file after it is read and before it is merged with the other data. (The expression should use column names alone; see Examples.) |
hush |
Logical. If |
This function is very similar to the readbulk::read_bulk
function. One important difference however is the data.table
use, which greatly speeds up the process. Another important difference is
the possibility of file selection based on any regex pattern.
Furthermore, this function allows pre-filtering by file (see filt).
Data files could include significant amount of unnecessary data, and
filtering prevents these to be merged.
data.table::rbindlist
# first, set current working directory
# e.g. to script's path with setwd(path_neat())
# read all text files in currect working directory
merged_df = read_dir("\\.txt$")
# merged_df now has all data
# to use utils::read.table for reading (slower than fread)
# (with some advisable options passed to it)
merged_df = read_dir(
'\\.txt$',
reader_function = read.table,
header = TRUE,
fill = TRUE,
quote = "\"",
stringsAsFactors = FALSE
)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.