apply_to_files: Apply a function to every file in a folder that matches a...

View source: R/file_tools.R

apply_to_filesR Documentation

Apply a function to every file in a folder that matches a regex pattern

Description

Create a list of files that match a regex search pattern, and then apply a function to each file. For example, run read_csv on every .csv file in a folder.

Usage

apply_to_files(
  path,
  pattern,
  func,
  ...,
  recursive = FALSE,
  ignorecase = TRUE,
  method = "full_join"
)

Arguments

path

(Character) The path to the folder.

pattern

(Character) A regular expression search pattern.

func

(Name) The bare name of a function to execute on each file.

...

(...) Optional arguments that will be passed to func.

recursive

(Logical) If TRUE, also search inside the subfolders of path.

ignorecase

(Logical) If TRUE, pattern is case-insensitive.

method

(Character) The method to use to merge all of the files into one dataframe. "full_join" (the default) returns all columns and rows. "left_join" returns all rows from the first file, and all columns from subsequent files. "inner_join" returns rows from the first file that have matches in subsequent files. "row_bind" simply appends each file to the last row of the dataframe, and leaves NAs when the files contain different columns.

Value

Invisibly returns a single dataframe with all of the input files merged together. If ⁠method = "row_bind",⁠ then a new column, orig_source_file, contains the source file's name. The "join" methods do not have this column because the values are mixed together.

Authors

Source

http://stackoverflow.com/a/24376207

Examples


# rain <- apply_to_files(path = "Raw data/Rainfall", pattern = "csv",
#                        func = readr::read_csv, col_types = "Tiic",
#                        recursive = FALSE, ignorecase = TRUE,
#                        method = "row_bind")

# dplyr::sample_n(rain, 5)

#> # A tibble: 5 x 5
#>
#>   orig_source_file       Time                 Tips    mV Event
#>   <chr>                  <dttm>              <int> <int> <chr>
#> 1 BOW-BM-2016-01-15.csv  2015-12-17 03:58:00     0  4047 Normal
#> 2 BOW-BM-2016-01-15.csv  2016-01-03 00:27:00     2  3962 Normal
#> 3 BOW-BM-2016-01-15.csv  2015-11-27 12:06:00     0  4262 Normal
#> 4 BIL-BPA-2018-01-24.csv 2015-11-15 10:00:00     0  4378 Normal
#> 5 BOW-BM-2016-08-05.csv  2016-04-13 19:00:00     0  4447 Normal


DesiQuintans/desiderata documentation built on April 9, 2023, 5:43 a.m.