How to open data frames from list using for loop?

Hi, I have a following problem:

I have a list that includes several dataframes. I would like to read each of them using for loop.

See bellow:

file.list <- list.files(pattern='*.xls.*', recursive = TRUE)
df.list_excel <- lapply(file.list, read_excel)


for(i in 1:length(df.list_excel)) {
  
name = paste0("file_", i)
name <- df.list_excel[[i]] 
 
  }

Because length(df.list_excel) = 13, I would like to have data frames called file_1, ..., file_13 imported. My loop opens only last df and call it "name", not file_13.

What do I do wrong, please?

You probably need assign for this. So, instead of

name = paste0("file_", i)
name <- df.list_excel[[i]] 

you could do something like

assign(paste0("file_", i), df.list_excel[[i]])

Right now you might be running the loop 13 times, but everything goes into name so you would not see the other ones.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.