1

I have a list of more than 50 csv data frames. I am listing my files from folder using:

#list files   
estaciones <- list.files(path = "Path",
                                 recursive = T, pattern = ".csv", full.names = T)



head(estaciones)

[1] "D:/A/Stations/AngosturaCochea_108013/Angostura_Cochea.csv"      
[2] "D:/A/Stations/BocadeTrampa_162003/BocadeTrampa_162003.csv"      
[3] "D:/A/Stations/Caimito_140005/Caimito_140005.csv"      


#read csvs
estaciones2 = lapply(estaciones, read.csv)

I want to set the names to each of my elements in the list to its original CSV file name.

I have tried using this but its not really doing it for me:

estaciones3 <- setNames(estaciones2, substr(list.files("Path", pattern=".csv"))

How can I assign names of files to my list elements?

Adi
  • 191
  • 7
  • can you provide a small sample? or show what your desired output looks like? – D.J Jul 26 '21 at 13:58
  • I've edited my post to show the head of my list. What I am looking for is that instead of a number [1] in my list for it to be the same of the file. For example in element [1] for it to be [Angostura_cochea]. Is this possible? – Adi Jul 26 '21 at 14:05

1 Answers1

2

You can use -

estaciones2 = lapply(estaciones, read.csv)
names(estaciones2) <- tools::file_path_sans_ext(basename(estaciones))

where tools::file_path_sans_ext would return the filename without extension i.e csv.

Ronak Shah
  • 377,200
  • 20
  • 156
  • 213