Bonono Bonono - 14 days ago 6x
R Question

Import data files in to a list based on a list of paths

I have a load of .csv data files that are organised per category. I want to import them in to a list per category.

To do this, I've created a list of path files per category:

path_list <- list(a = c("path1","path2"), b= c("path3", "path4","path5"))

I then want read in the ".csv" files in to a new list that has the same structure as

data_list <- vector("list", length(path_list))

names(data_list) <- names(path_list)

for(i in 1:length(data_list)) {
for (j in 1:3)) {

data_list[[i]] <- read.csv(path_list[[i]][[j]], header=T, na.strings = "", stringsAsFactors = F)

print (i)


This works for the first two elements of
but then I get the error:

Error in path_list[[i]][[j]] : subscript out of bounds

Which I know is because
only has two elements in a when actually
. Is there a way of making
dynamic for each list element?

End result should look like:

[1] "datafile1" "datafile2"

[1] "datafile3" "datafile4" "datafile5"


It is out-of-bound because not all elements of path_list is a length-3 vector. The following works:

data_list <- setNames(vector("list", length(path_list)), names(path_list))
l <- lengths(path_list)    ## length of each vector

for(i in 1:length(data_list)) {
  tmp <- vector("list", l[i])
  for (j in 1:l[i])) {
     tmp[[j]] <- read.csv(path_list[[i]][j], header=TRUE, na.strings = "", stringsAsFactors = FALSE)
  data_list[[i]] <- tmp