'How to load multiple csv files into seperate objects(dataframes) in R based on filename?

I know how to load a whole folder of .csv files quite easily using:

csv_files = list.files(pattern ="*.csv")
myfiles = lapply(csv_files, read.delim, header = FALSE)

From which I can then easily iterate over 'myfiles' them and do whatever I wish. The problem I have is this simply loads all the .csv files in the working directory.

What I would like to do is be able to assign the files to objects in the script based on the filename.

Say, for example, in one directory I have the files; file001, file002, file003 and exfile001, exfile002, exfile003.

I want to be able to load them in such away that

file_object <- file...
exfile_object <- exfile...

So that when I execute the script it essentially does whatever i've programmed it to do for file_object(assigned as file001 in this example) & exfile_object(assigned as exfile001 in this example). Then goes on to continue in this way for the rest of the files in the directory (eg. file002, exfile002, file003, exfile003).

I know how to do it in MATLAB, but am just getting to grips with R.

I thought perhaps getting them into seperate lists using the list.files function may work by just changing working directory in script, but it seems messy and would involve re-writing things in my case...

Thanks!



Solution 1:[1]

If your list of frames, myfiles is named using this:

names(myfiles) <- gsub(".csv", "", csv_files)

then you can do

list2env(myfiles, globalenv())

to convert those individual frames to separate objects in the global environment.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 langtang