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?