0

I want to merge multiple csv files stored in a single folder, and to add a column containing the name of each csv file on the rows that correspond to it. The files have identical numbers of columns and column names. For example:

File 1.csv Row 1, column 1: "a", row 1, column 2: "b"
File 2.csv Row 1, column 1: "c", row 1, column 2: "d"

Desired output:

Column 1 Column 2 Column 3
  a.       b.      File 1.csv
  c.       d.      File 2.csv
MrFlick
  • 195,160
  • 17
  • 277
  • 295
Marina
  • 1
  • 1
  • 1
  • 1
    do you want to `merge` the files, or `append` them? merging (roughly) means adding new columns; appending means adding new rows. Say you had files A and B with 50 and 100 rows and 10 columns each. the `merge` output could have 50-150 rows and 10-20 columns; the append output would have 150 rows and 10 columns. – MichaelChirico Jul 24 '20 at 11:27

2 Answers2

3

You can first get all the file names which you want specifying the pattern argument in list.files. You can read them using lapply and add a new column.

files <- list.files(pattern = '\\.csv$', full.names = TRUE)

all_data <- do.call(rbind, lapply(files, function(x) 
                    transform(read.csv(x), File = basename(x))))

You can also do this with tidyverse :

library(dplyr)
library(purrr)

all_data <- map_df(files, ~read.csv(.x) %>% mutate(File = basename(.x)))
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
0

This can be done in various ways. The method I tend to use fs to get a dataframe of the file paths, and then use purrr to read the csv's in as a nested data frame:

library(tidyverse)
library(fs)
dir_info() %>%
  filter(endsWith(path, ".csv")) %>%
  select(path) %>% 
  mutate(data = purrr::map(path, read_csv)) %>%
  unnest()
Robert Wilson
  • 3,192
  • 11
  • 19