问题描述
我正在尝试使用 R 合并多个 csv 文件.所有 CSV 文件都具有相同的字段,并且都是仅包含这些 CSV 文件的共享文件夹.我尝试使用以下代码来做到这一点:
I'm attempting to merge multiple csv files using R. all of the CSV files have the same fields and are all a shared folder only containing these CSV files. I've attempted to do it using the following code:
multmerge=function(mypath) {
filenames=list.files(path=mypath, full.names=TRUE)
datalist= lapply(filenames, function (x) read.csv(file=x, header=True))
Reduce(function(x,y) merge(x,y), datalist)}
我输入的路径类似于Y:/R Practice/specdata".当我将函数应用到我的 300 个左右的 csv 文件时,我确实得到了一个输出,但结果给了我我的列名,但在它下面有 (或 0 长度的 row.names).如果您对为什么这不起作用以及我如何解决它有任何建议,请告诉我.
I am entering my path as something like "Y:/R Practice/specdata".I do get an ouput when I apply the function to my 300 or so csv files, but the result gives me my columns names, but beneath it has <0 rows> (or 0-length row.names).Please let me know if you have any suggestions on why this isn't working and how I can fix it.
推荐答案
更短、更快的解决方案
library(dplyr)
library(readr)
df <- list.files(path="yourpath", full.names = TRUE) %>%
lapply(read_csv) %>%
bind_rows
这篇关于尝试在 R 中合并多个 csv 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!