0

I have a folder that contains multiple files like that:

output_1_NS.csv 
output_1.csv
output_2_NS - output_2.csv
output_2.csv
...
output_67.csv

I want to read in all the files with the string 'NS' contained in the file name. The end product should be one dataframe with all the files containing 'NS' appended together.

How may I do this efficiently?

Thank you!

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
jo_
  • 677
  • 2
  • 11
  • 1
    Use the **`fs`** package for the file system: with `file_paths <- fs::dir_ls(path = "path/to/your/folder", regexp = "NS")` you can list all such files in your directory. Then use the **`purrr`** package for iteration and the **`readr`** package for reading the CSVs: with `purrr::map(file_paths, readr::read_csv, ...)` you will get a `list` of the datasets extracted from those files, where `...` would contain further arguments to control the extraction. You can also do these things with base R: `ls()` and `sapply()` and `read.csv()`. But the earlier packages are best practice in the tidyverse. – Greg Jun 20 '23 at 18:56
  • 2
    readr works with file lists - https://stackoverflow.com/a/61511379/646761 – margusl Jun 20 '23 at 19:04

0 Answers0