How to Combine Multiple .CSV Files in R

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



Leave a reply



Submit