0

I have 54 CSVs with identical column headers. I need to do the following but have been unable to find an efficient way to do it (ideally I would do this in a single loop command):

1.) read in the CSVs into separate data.frames, with the data.frames titled the same as the CSVs they come from
2.) create a new column in each, titled "Name"
3.) in each data.frame, populate the cells of the new "Name" column with the title of that data.frame
4.) stack these data.frames into a new one

I have tried making for loops, which seem like the best way to go, without success. I'd be happy to hear other ways it could be done. Thanks a lot in advance!

  • 1
    Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the [How to Ask](https://stackoverflow.com/help/how-to-ask) page for help clarifying this question. – markus Mar 30 '19 at 19:41
  • This post seems to be a good starting point: [How to import multiple .csv files at once?](https://stackoverflow.com/questions/11433432/how-to-import-multiple-csv-files-at-once) – markus Mar 30 '19 at 19:42

1 Answers1

1

Even though this is very doable in base R, your quickest / sanest option is to load at least dplyr and possibly purrr and readr (all part of the tidyverse suite of packages). If you haven't installed them already, you proceed with:

install.packages(c("dplyr", "purrr", "readr"))

or alternatively install tidyverse (that contains all of the above + more)

install.packages("tidyverse")

You then load them into your session.

library(purrr)
library(dplyr)
library(readr)

You then tell the function list.files where your csv files are stored and read them in using map and read_csv. The %>% symbol is a pipe that takes output from the previous function and pipes it as input into the next. Obviously change "/path/to/your/files/" to the actual directory.

files <- list.files(path = "/path/to/your/files/", pattern = "csv", full.names = TRUE)
files %>% 
  map(.f = ~read_csv(.x)) %>%
  set_names(nm = files) %>%      #you can also change the names manually
  map_dfr(identity, .id = "Name")
biomiha
  • 1,358
  • 2
  • 12
  • 25