Blaze9
Blaze9

Reputation: 193

Read in files from list of lists

I have a bunch of files for which I created lists for such as below:

a_files <- list.files(path="./Afiles/", full.names = T)
b_files <- list.files(path="./Bfiles/", full.names = T)
c_files <- list.files(path="./Cfiles/", full.names = T)
d_files <- list.files(path="./Dfiles/", full.names = T)
e_files <- list.files(path="./Efiles/", full.names = T)

And a list of the following lists:

list_of_lists <- list(a_files, b_files, c_files, d_files, e_files)

I'm trying to now read in each of those lists to their own respective dataframes as follows:

for (i in 1:length(list_of_lists)){
 temp <- fread(list_of_lists[i], stringsAsFactors = F) 
 i <- rbindlist(list(i, temp_data), use.names = T)
}

But I get an error:

Error in fread(file_list[i], stringsAsFactors = F) : 
  input= must be a single character string containing a file name, a system command containing at least one space, a URL starting 'http[s]://', 'ftp[s]://' or 'file://', or, the input data itself containing at least one \n or \r
>

Any idea on what I'm doing wrong, and how to properly get this done?

In the end the I want dataframes (a_files, b_files, c_files) containing the contents of each folder. The files in each folder are of identical column width (all A files have the same 10 columns, with varying number of rows, all B files have the same 40 columns with varying number of rows, etc.)

Upvotes: 0

Views: 237

Answers (1)

Ronak Shah
Ronak Shah

Reputation: 388817

fread (or any other way to read data) reads only one file at a time. You can try.

library(data.table)
group_lists <- lapply(list_of_lists, function(x) rbindlist(lapply(x, fread)))

You can use this list for further manipulation or if you want them as separate dataframe, you can assign names to them and then use list2env

names(group_lists) <- paste0(letters[1:5], '_files')
list2env(group_lists, .GlobalEnv)

Upvotes: 1

Related Questions