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
How to Set the Default Language of Date in R
Is There a Weighted.Median() Function
How to Convert Integer into Categorical Data in R
Join R Data.Tables Where Key Values Are Not Exactly Equal--Combine Rows with Closest Times
Select Row with Most Recent Date by Group
Add a Box for the Na Values to the Ggplot Legend for a Continuous Map
Modify X-Axis Labels in Each Facet
Perform Multiple Paired T-Tests Based on Groups/Categories
How to Pivot/Unpivot (Cast/Melt) Data Frame
How to Color Sliderbar (Sliderinput)
Function to Split a Matrix into Sub-Matrices in R
How to Do Range Grouping on a Column Using Dplyr
Plot a Line Chart with Conditional Colors Depending on Values
Rolling Sum by Another Variable in R
Apply a Ggplot-Function Per Group with Dplyr and Set Title Per Group
Is There an R Function to Reshape This Data from Long to Wide