0

I am trying to iterate over measurements and append all the data to one list, which I could later plot. Everything is done in R.

 for (file in filenames){  
    filename <- file  

    data <- read_delim(file = file, delim = ";", col_types = cols(  
       name = col_character(),  
       surname = col_character(),  
    )) %>% mutate(filename = filename) //integer since the files are saved with timestamp

}

So from the initial version of the file that I read, I want to append an additional column "filename" where I would save the name of the file. My problem is that apparently in every iteration it overwrites the previously saved values ending up with a list where all entries have the name of the last file that was read.

Does someone have an idea how could I improve this?

Update: I am trying to append the rows vertically. Not to create a list of lists

CroatiaHR
  • 615
  • 6
  • 24
  • 1
    Related, possible duplicate: https://stackoverflow.com/questions/11433432/how-to-import-multiple-csv-files-at-once See this post for reading multiple files. – zx8754 Mar 11 '20 at 21:24
  • @StupidWolf, my comment (on your answer) on growing the object iteratively was about `rbind`ing in-place, not doing what you're doing here. What you're doing here is a better more-immediate translation of what the user asked. – r2evans Mar 11 '20 at 21:37
  • 1
    @r2evans, no worries, I got what you mean, in terms of not continuously rbind-ing the object. Your answer is what I normally do. So to avoid confusion for OP, I just delete my post. – StupidWolf Mar 11 '20 at 21:45
  • thank you guys for the effort – CroatiaHR Mar 11 '20 at 21:58

1 Answers1

2

I suggest you work instead through a list of frames, doing more processing after the fact. (See: https://stackoverflow.com/a/24376207)

alldat <- lapply(filenames, readr::read_delim, delim=";",
                 col_types=cols(name = col_character(), surname = col_character()))

will get you a frame with all of the data. From there, you can put everything together with:

# choose one
combineddat <- do.call(rbind, Map(transform, alldat, filename = filenames))
combineddat <- dplyr::bind_rows(Map(transform, alldat, filename = filenames))
combineddat <- data.table::rbindlist(Map(transform, alldat, filename = filenames))
r2evans
  • 141,215
  • 6
  • 77
  • 149