0

I have a directory path

directory<-"C:/User/Files"

in the directory i have these files

dir(directory)

"R_folder","Roster_NBA1.csv", "Roster_NBA2.csv", "Roster_NBA3.csv", "Roster_NBA3.csv","Roster_NBA4.csv"

I would like to write a script that loops through the whole directory and pulls the data that starts in Roster and assigns NBA1, NBA2, NBA3, and NBA4 df from the csv files. At the moment I have something like this I couldn't figure out how to say if it starts with word Roster then run through loop where you assign a data frame based on right 4 letters or starting from NBA

for (i in grepl('^Roster',dir(directory))) { Add expression here}
user35131
  • 1,105
  • 6
  • 18
  • Your question is similar to these: https://stackoverflow.com/questions/65865409/read-multiple-files-but-keep-track-of-which-file-is-which-dataframe-in-r/65865668#65865668 https://stackoverflow.com/questions/65865409/read-multiple-files-but-keep-track-of-which-file-is-which-dataframe-in-r/65865668#65865668 checking these may be helpful – GuedesBF Feb 05 '21 at 21:37
  • Why not keep data.frames in a named list? – Michael M Feb 05 '21 at 21:46
  • 1
    On @MichaelM's comment, see https://stackoverflow.com/a/24376207/3358227 – r2evans Feb 05 '21 at 21:51
  • 1
    BTW, `grepl` returns `logical`, so `for (i in grepl(..))` is just going to iterate (with no further context) on a sequence of true/false/..., which is not very useful. I think what you're trying to do there is likely `for (path in grep("^Roster", list.files(directory, full.names=TRUE), value=TRUE) { do_something; }` – r2evans Feb 05 '21 at 21:52

2 Answers2

3
files <- list.files(directory, pattern = "^Roster_.*\\.csv$", full.names = TRUE)
alldat <- lapply(files, read.csv)
names(alldat) <- gsub("^Roster_|\\.csv$", "", files)

Since the data are all related, I suspect that whatever you're going to do to one of the frames will be done to all of them. Given that context, instead of assigning them as individual objects in your environment and doing this

NBA1 <- do_something(NBA1, arg1=T)
NBA2 <- do_something(NBA2, arg1=T)
NBA3 <- do_something(NBA3, arg1=T)
...

You can simply do

alldat <- lapply(alldat, function(dat) do_something(dat, arg1=T))

and get all frames in one step. See https://stackoverflow.com/a/24376207/3358227 for more discussion on that point.

r2evans
  • 141,215
  • 6
  • 77
  • 149
2

This should work

setwd("C:/User/Files")
#create a vector of files that you want to read    
filestoread=list.files(pattern="Roster")
#read the files to a list
filelist=lapply(filestoread, read.csv)
#change the names of the dfs of the list to NBA something
names(filelist)=gsub("Roster_", "", filestoread);
names(filelist)=gsub(".csv", "", filestoread)
#split the list into separate dfs in the global environment
#actually is better to keep them in the list but if you need them separate
list2env(list_a, envir = .GlobalEnv)
biofelip
  • 81
  • 5