尝试在R中合并多个csv文件 [英] Trying to merge multiple csv files in R

查看:1866
本文介绍了尝试在R中合并多个csv文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用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 rows>(或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.

推荐答案

对于更短,更快的解决方案

For a shorter, faster solution

library(dplyr)
library(readr)
df <- list.files(full.names = TRUE) %>% 
  lapply(read_csv) %>% 
  bind_rows 

这篇关于尝试在R中合并多个csv文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆