Trying to Merge Multiple CSV Files in R

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))

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)

How to simultaneously merge multiple csv files and summarize several variables per group

You can try the following part of the code assuming all your csv files that you want to combine are in the working directory itself.

library(tidyverse)

myfiles <- list.files(pattern = '.csv')

map_df(myfiles, function(x) {
year_number <- readr::parse_number(x)
df <- read.csv2(x)
df %>%
mutate(Total = rowSums(select(., -(1:5)), na.rm = TRUE)) %>%
pivot_longer(cols = starts_with('country')) %>%
group_by(name, value) %>%
summarise(Total = sum(Total)) %>%
pivot_wider(names_from = name, values_from = Total) %>%
mutate(year = year_number)
}) %>%
arrange(country, year) -> result

result

How to load and merge multiple .csv files in r?

It appears as if you might be trying to use the nice function shared on R-bloggers (credit to Tony Cookson):

multMerge = function(mypath){
filenames = list.files(path = mypath, full.names = TRUE)
datalist = lapply(filenames,
function(x){read.csv(file = x,
header = TRUE,
stringsAsFactors = FALSE)})
Reduce(function(x,y) {merge(x, y, all = TRUE)}, datalist)
}

Or perhaps you have pieced things together from difference sources? In any event, merge is the crucial base R function that you were missing. merge_all doesn't exist in any package.

Since you're new to R (and maybe all programming) it's worth noting that you'll need to define this function before you use it. Once you've done that you can call it like any other function:

my_data <- multMerge("/home/sjclark/demographics/")

How to merge many .csv files into R excluding the top three rows?

you can skip the first 3 rows using skip parameter of read.cvs. You could check ?read.cvs. In case you need the data from the first rows as well, you could use two separate read.cvs commands, as suggested here.

P.S. Your question seems to be about programming in R and not about statistics, so I flagged it as off-topic to be moved to Stackoverflow.



Related Topics



Leave a reply



Submit