Learn R Programming

HelpersMG (version 6.2)

read_folder: Read files present in a folder and creates a list with the content of these files

Description

To create a list, the syntax is:
datalist <- read_folder(folder=".", read=read.delim, header=FALSE)
It returns an error if the folder does not exist.
The names of the elements of the list are the filenames.
The parameter file can be used to predefine a list of file. If file is NULL, all the files of the folder/directory are used.

Usage

read_folder(
  folder = try(file.choose(), silent = TRUE),
  file = NULL,
  wildcard = "*.*",
  read = read.delim,
  ...
)

Value

Return a list with the data in the files of the folder (directory for windows users)

Arguments

folder

Where to search for files; can be or a file path or a folder path

file

list of files

wildcard

Define which files are to be read (examples: ".", ".xls", "essai.txt"). It can be also a vector with all filenames.

read

Function used to read file. Ex: read.delim or read.xls from gdata package

...

Parameters send to the read function

Author

Marc Girondot marc.girondot@gmail.com

Details

read_folder reads all files present in a folder

Examples

Run this code
if (FALSE) {
library(HelpersMG)
# Read all the .csv files from the current folder/directory
contentaslist <- read_folder(folder=".", wildcard="*.csv", read=read.csv2)
# Read all the files from the current folder/directory
contentaslist <- read_folder(folder=".", wildcard="*.*", read=read.csv2)
# Read two files from the current folder/directory
files <- c("filename1.csv", "filename2.csv")
contentaslist <- read_folder(folder=".", wildcard=files, read=read.csv2)
# To concvert the list into a single dataframe:
mydf <- do.call("rbind", contentaslist)
}

Run the code above in your browser using DataLab