Home > Back-end >  How to load multiple csv files into seperate objects(dataframes) in R based on filename?
How to load multiple csv files into seperate objects(dataframes) in R based on filename?

Time:03-05

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!

CodePudding user response:

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.

CodePudding user response:

Solution for anyone curious...

files <- list.files(pattern = ".*csv")

for(file in 1:length(files)) { 
  file_name <- paste(c("file00",file), collapse = " ")
  file_name <- gsub(" ", "", file_name, fixed = TRUE)
  ex_file_name <- paste(c("exfile00",file), collapse = " ")
  ex_file_name <- gsub(" ", "", ex_file_name, fixed = TRUE)
  
  file_object <- read.csv(file = paste(file_name, ".csv", sep=""),fileEncoding="UTF-8-BOM")
  exfile_object <- read.csv(file = paste(ex_file_name, ".csv", sep=""),fileEncoding="UTF-8-BOM")
  }

Essentially build the filename within the loop, then passs it to the readcsv function on each iteration.

  • Related