I have a folder with many csv files. Each csv file has the same number of columns. I would like to combine them creating a column with the file name. I've seen similar post about it but I cannnot get it to work. I'm trying the following script. Any help will be much appreciated
library(plyr)
csvfilenames <- list.files("/Users/PAM/Desktop/CSVFilesToMerge/",
pattern="*.csv", all.files=FALSE, full.names=FALSE)
csvfilenames
library(plyr)
CombinedData = ldply(csvfilenames, function(filename) {
dum = read.table(filename, header=TRUE, fill = TRUE, sep=";")
dum$Filename = csvfilenames
return(dum)
})
CombinedData`
This is my next attempt with purrr but it doesnt work
library(readr) # for read_csv()
library(purrr) #for map(), reduce()
data_path<-"/Users/PAM/Desktop/TestCSVFilesToMerge/"
files <- dir(data_path, pattern = "*.csv") # get file names
files
data <- files %>% # read in all the files, appending the path before the filename
map(~ read_csv(file.path(data_path, .))) %>%
reduce(rbind)
data