read_dir {neatStats} | R Documentation |
Read and Merge Files from Directory
Description
Reads data files from any given directory as data frames and
merges them into a single data frame (using
data.table::rbindlist
).
Usage
read_dir(
pattern = "*[.]",
path = ".",
reader_function = data.table::fread,
...,
subdirs = FALSE,
filt = NULL,
hush = FALSE
)
Arguments
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 |
Note
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.
See Also
Examples
# 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
)