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

I am relatively new to R and I am having a problem reading multiple tables from a directory using the apply function. What I would like to use is to use a vector with the paths to the tables I am interested in and generate a list with objects consisting of each data frame corresponding to the paths in that file. I wrote 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" ))
}

      

According to my understanding, I am doing the creation of a list of filenames in the directory I want by creating a path to those files and (where I fail) iterate over those paths and import the tables they match to the entire file.paths object and generate a list with those tables. I am getting the following error:

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

      

Can anyone suggest any advice?

+3


source to share


1 answer


Here are several options depending on what you want:

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 am assuming your data files are saved in .csv

. Note that it is fread

equivalent read.table

but much faster



Concatenate multiple data frames in one 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))

      

Loading multiple data frames into different objects in the 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)

      

+7


source







All Articles