How to read in multiple data tables into R using apply function?

sam picture sam · May 22, 2015 · Viewed 8.1k times · Source

I am relatively new to R and I'm having a problem reading in multiple tables from a directory using an apply function. What I would like to have the function do is to use a vector with paths to tables that I'm interested in and generate a list with objects consisting of each data frame corresponding the paths in that file. I've written the following code:

f<- function(directory){
    file.list <<- list.files(directory)
    file.paths <<- as.vector(paste(directory, file.list, sep = "/"))
    tables <- lapply(X = file.paths, FUN = read.table, header = TRUE,sep = "\t" ))
}

By my understanding, what I'm doing is creating a list of file names in the directory that I want, creating a path to those files, and (where I'm failing is) looping over those paths and importing the tables they correspond to for the whole file.paths object and generating a list with those tables. I receive the following error:

Error in FUN(X[[i]], ...) : no lines available in input

Can anyone offer any advice?

Answer

rafa.pereira picture rafa.pereira · Sep 29, 2015

Here are a few options depending on what you want the output to be:

A list of data frames

# Load library
  library(data.table)

# Get a List of all files named with a key word, say all `.csv` files
  filenames <- list.files("C:/your/folder", pattern="*.csv", full.names=TRUE)

# Load data sets
  list.DFs <- lapply(filenames,fread)

I'm assuming your data files are saved in .csv format. Note that fread is equivalent to read.table but much much faster

Bind multiple data frames into one single data frame

# Get a List of all files named with a key word, say all `.csv` files
  filenames <- list.files("C:/your/folder", pattern="*.csv", full.names=TRUE)

 # Load and bind all data sets
   data <- rbindlist(lapply(filenames,fread))

Load multiple data frames as different objects to Global Environment

# Get a List of DF in the directory
  filenames <- list.files("C:/your/folder", pattern="*.Rda", full.names=TRUE)

# Load data sets
  lapply(filenames, load, .GlobalEnv)