0

I do not know how to create a reproducible example of this issue since it would require access to a folder on my computer(if anyone has a suggestion I am fine to follow up). However, what I would like to do is upload a file in a folder without actually referencing the name but only referencing the order it appears in the file, and I would like to do it in R if possible(although python would be fine as well). I want to do this because the folder has 40 or so files and I need to format them all the same, but they all have different names.

In other words I want a code that would look something like this:

setwd(folder)

for(i in 1:number of files in folder){

upload file i

process file i

rbind(master file,file i)

}

Obviously this is not an actual code, but merely a framework, so I did not put it in the code framework on the site. The line I do not know how to do is the first line in the loop(download file i). Is it possible to do this, or do I need to download every file individually with their specified name?

Rajith Thennakoon
  • 3,975
  • 2
  • 14
  • 24
MathStudent
  • 129
  • 11
  • `list.files()` ? – Edward Apr 22 '20 at 00:40
  • "Upload" to where? How? – r2evans Apr 22 '20 at 00:42
  • 2
    Using Edward's start, `results <- lapply(list.files(folder, full.names = TRUE), function(fn) { dat <- read.csv(dat); process_data(dat); })` might be a good start. Do not iteratively grow your data with `rbind` here, it scales horribly (and is prone to other problems). Instead, get a list-of-frames (as I just suggested), then finish up with `do.call(rbind, results)` or `dplyr::bind_rows(results)` or `data.table::rbindlist(results)`. – r2evans Apr 22 '20 at 00:43
  • See chapter 2, *"Growing Objects"*, of the [R Inferno](https://www.burns-stat.com/pages/Tutor/R_inferno.pdf). And then look at https://stackoverflow.com/a/24376207/3358272, where it talks about reading multiple files into frames and processing them. – r2evans Apr 22 '20 at 00:44

1 Answers1

1

I think you're looking for list.fles()

ff <- list.files()

for(i in seq_along(ff)){
  print(ff[i])
  read.csv(ff[i], ...) # etc

  ...

}
Edward
  • 10,360
  • 2
  • 11
  • 26