Read multiple files but keep track of which file is which dataframe in R

Gabriel G.

Let's say I have these files:

N1.xlsx
N2.xlsx
N3.xlsx
N4.xlsx

I want them in a list, but each dataframe must be named according to the file it was read from, like

mylist = 
N1
N2
N3
N4

I'm using:

fnames =  mixedsort(sort(list.files("filepath", pattern = '*.xlsx', full.names = F)))

mylist <- lapply(fnames, function(x) {
 read_xlsx(paste0(x),  col_names = TRUE)
})

But this code creates a list without identification

mylist = 
[[1]]
[[2]]
[[3]]
[[4]]

Its important to keep the names of each file in each dataframe, so I can export them correctly later!

Ronak Shah

You could name mylist with the names you already have in fnames.

names(mylist) <- tools::file_path_sans_ext(fnames)
mylist

file_path_sans_ext remove extension from the filenames.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Track which files has been read and loaded into db

How to delegate threads to multiple servers and keep a track in which thread is on which server using Java

Keep track of which UITextField is currently in editing mode

Keep track of which CheckBox has been Checked

read excel file which has = in a column in r

How do I keep track of which icon is currently selected instead of multiple icons at a time

Extract comments from R source files, keep function in which they occurs

Read csv with json column which is nested multiple times into dataframe

Read Content from Files which are inside Zip file

Can i read from .ini file which located in resources files?

Track file in master repository which is ignored in submodule

Python: Read csv file of which one column contains multiple commas

Find which files are read or written to

Convert JSON File which contains multiple dictionary to pandas dataframe

How to convert python file into exe which has multiple other files with it?

How can I keep track of which text entry is clicked?

How does Rails keep track of which migrations have run for a database?

Which software can I use to keep a track of color percent?

how to keep track of image which should not goes out of the container

Is there a way to name a column after a text file from which it was read in R?

Read multiple csv files (and skip 2 columns in each csv file) into one dataframe in R?

which file or directory is not able to read?

only keep words in a list which is an element of dataframe

Keeping Track of Excel Files Which Have Already been Processed VBA

How to read content of files inside Zip file which is inside a Zip file

Check if process is running on multiple remote computers and copy a file if process is NOT running, keep list of those which succeeded

Read multiple XML files, cleanup and merge with existing dataframe in R studio

Read a file using Stream and keep track of the current index

Which files? Which subroutines?