Merge multiple .csv files into one
# Get file list
file_list <- list.files()
# Read all csv files in the folder and create a list of dataframes
ldf <- lapply(file_list , read.csv)
# Combine each dataframe in the list into a single dataframe
df.final <- do.call("rbind", ldf)
Import and combine multiple .csv files and create multiple dataframes
You can store the dataframes in a list after reading them, and set up a simple for loop:
n = 10 # fill with number of weeks
file_list = list()
for (i in seq(n)) {
file_list[[i]] <- list.files(path = 'x:/full/file/path',
pattern = str_c("^Dai15E_ABC_10mbin_20211201_fullwatercolumn_evening_BNR*.*_week", i, ".csv"), full.names = TRUE) %>%
map_dfr(read_csv)
}
merge multiple .csv files - R
You can use :
data_csv <- do.call(rbind, lapply(myfiles, read.csv, sep = ";"))
Or with purrr
's map_df
data_csv <- purrr::map_df(myfiles, read.csv, sep = ";"))
If there are lot of files you can use data.table
functions.
library(data.table)
data_csv <- rbindlist(lapply(myfiles, fread))
Combining multiple csv files together in an r loop
Here are couple of ways to do count Type
column from each file, add a new column with the filename and bind the output together.
Using base R :
files = list.files(pattern = "*.csv", full.names = TRUE)
new_data <- do.call(rbind, lapply(files, function(x) {
mydata <- read.csv(x, skip=1, header = TRUE)
transform(as.data.frame(table(mydata$Type)),
filename = basename(x))
}))
and with tidyverse
:
library(dplyr)
new_data <- purrr::map_df(files, function(x) {
mydata <- read.csv(x, skip=1, header = TRUE)
mydata %>%
count(Type) %>%
mutate(filename = basename(x))
})
Combining multiple .csv files using row.names
We may do this in tidyverse
library(dplyr)
library(purrr)
map(count_lists, ~ .x %>%
rownames_to_column('rn')) %>%
reduce(full_join, by = 'rn') %>%
mutate(across(everything(), replace_na, 0))
Related Topics
How to Import Only One Function from Another Package, Without Loading the Entire Namespace
Multi Line Title in Ggplot 2 with Multiple Italicized Words
How to Combine Multiple .CSV Files in R
How to Optimize the Following Code with Nested While-Loop? Multicore an Option
R: How to Make a Confusion Matrix for a Predictive Model
How to Convert a Character String Date to Date Class If Day Value Is Missing
R Plot: Using Italics and a Variable in a Title
Running Out of Heap Space in Sparklyr, But Have Plenty of Memory
Display Frequency Instead of Count with Geom_Bar() in Ggplot
R System Functions Always Returns Error 127
How to Output a Stem and Leaf Plot as a Plot
Page Refresh Button in R Shiny
Inserting Rows into Data Frame When Values Missing in Category
Setting Individual Y Axis Limits with Facet Wrap Not with Scales Free_Y