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?
Here are a few options depending on what you want the output to be:
A list of data frames
I'm assuming your data files are saved in
.csv
format. Note thatfread
is equivalent toread.table
but much much fasterBind multiple data frames into one single data frame
Load multiple data frames as different objects to Global Environment