'Merging csv files in R and adding a column with file names [duplicate]

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
r


Solution 1:[1]

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)))

Solution 2:[2]

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()

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ronak Shah
Solution 2