0

I hope you can help me here, I would like to import several files stored in several folders and subfolders. Until now I either copy/pasted the results in one excel file or imported one file after another in R, but now I have to many files to deal with...

Here is what I need to do: 1 folder: Data --> containing 60 subfolders with subject ID: subj 1, subj 2, subj 3, ... --> each subfolders contained 6 excel files: T1, T2,t3,....

I would like to import all these excels files in R, either in one big data frame or one data frame for each subject. And even better would be to add a column with "Subject ID" (Subj1, subj2, subj3,...) and another one with Lap (T1, T2, T3....).

Does someone know how to do that? Thank you.

Virginie
  • 67
  • 5
  • Does this answer your question? [How can I read multiple (excel) files into R?](https://stackoverflow.com/questions/32888757/how-can-i-read-multiple-excel-files-into-r) – Limey Jun 08 '22 at 12:40
  • No because in this question, all the excel files are in the same folder, and I have different folder with several excel files in each of them... – Virginie Jun 08 '22 at 12:44
  • Yes, because you write a function to import all the files in one folder and then loop through all the folders of interest. – Limey Jun 08 '22 at 12:46
  • Ok ,I checked again, not sure I understand everything but I manage to do something actually. Thanks! Could you maybe just help me with where to add that I like to skip the first 2 rows in each excel file? – Virginie Jun 08 '22 at 13:07

1 Answers1

0
lapply(list.files(path="./folder/",pattern='subj', recursive=T),FUN=function(x) load(file=paste0("./folder/",x),.GlobalEnv))

It is not really clear the structure of your folder/subfolder/files, but I think these lapply function will help you. You have to specify the path, the pattern, and if you want it to look inside the subfolder (recursive). Then a function will load them all (in that case you have to specify a function that reads the type of file you want (e.g. load("...Rdata"), read.csv(), etc)

understorey
  • 124
  • 1
  • 10
  • I manage something like that: file.list <- list.files(pattern='*.CSV', recursive = TRUE) file.list <- setNames(file.list, file.list) df.list <- lapply(file.list, read_csv, skip=2) TTP <- bind_rows(df.list, .id = "id") But the separator is ";" and I don't know where to specify it, for now I have all the data in 1 column.... – Virginie Jun 08 '22 at 13:46