Read and Rbind Multiple CSV Files

Read and rbind multiple csv files

Find files (list.files) and read the files in a loop (lapply), then call (do.call) row bind (rbind) to put all files together by rows.

myMergedData <- 
do.call(rbind,
lapply(list.files(path = "N:/Ring data by cruise"), read.csv))

Update: There is a vroom package, according to the manuals it is much faster than data.table::fread and base read.csv. The syntax looks neat, too:

library(vroom)
myMergedData <- vroom(files)

Read multiple csv into one and add a new column based on the file names

You can use sapply to read all the files in a list and with rbindlist combine them into one dataframe with a new column filename which has name of the file in every row.

library(data.table)
result <- rbindlist(sapply(files, fread,simplify = FALSE), idcol = 'filename')

How can I read multiple csv files into R at once and know which file the data is from?

In case you want to use base R you can use

file.names <- list.files(pattern = "*.csv")

df.list <- lapply(file.names, function(file.name)
{
df <- read.csv(file.name)
df$file.name <- file.name
return(df)
})

df <- list.rbind(df.list)

Import and rbind multiple csv files with common name in R

Do you look for something like this?

do.call(rbind, lapply(list.files(path=".", pattern="AM-25"), read.table, header=TRUE, sep=","))

This would rbind together the matrices read from your csv files which contain the characters "AM-25".
The arguments for read.table could be different, depending on your csv files.


EDIT

I hope this works for the case that you do not know all possible five-letter prefixes of filenames in your directory:

##Get all different first five letter strings for all cvs files in directory "."                                                                                                                                                                                           
file.prefixes <- unique(sapply(list.files(path=".", pattern="*.csv"), substr, 1,5))

##Group all matching file names according to file.prefixes into a list
file.list <- lapply(file.prefixes, function(x)list.files(pattern=paste("^",x,".*.csv",sep=""), path="."))
names(file.list) <- file.prefixes ##just for convenience

##parse all csv files in file.list, create a list of lists containing all tables for each prefix
tables <- lapply(file.list, function(filenames)lapply(filenames, function(file)read.table(file, header=TRUE)))

##for each prefix, rbind the tables. Result is a list of length being length(file.prefixes)
## each containing a matrix with the combined data parsed from the files that match the prefix
joined.tables <- lapply(tables, function(t)do.call(rbind, t))

##Save tables to files
for (prefix in names(joined.tables))write.table(joined.tables[[prefix]], paste(prefix, ".csv", sep=""))

add several csv files in the end of each other using R

We can get load the '.csv' files in a list, read them with read_csv (from readr) by loopiing over the files with imap

library(purrr)
library(readr)
files <- list.files(pattern = '\\.csv', full.names = TRUE)
names(files) <- tools::file_path_sans_ext(basename(files))
out <- imap_dfr(files, read_csv, .id = 'age')


Related Topics



Leave a reply



Submit