在R中合并多个csv文件 [英] Merging multiple csv files in R

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

问题描述

我有大约100个csv文件与公共标题,我想要合并。标题为Lat,Long和value。我正在尝试合并所有的csv文件,以便输出将是

 LatLonValue1Value2 ...价值100

Lat Lon 列对于所有csv文件是相同的。合并两个文件很容易

  merge(data.frame1,data.frame2,by = c('Lat','Lon' ))

但是,我尝试了以下没有工作的代码:

  file_list<  -  list.files(〜/ source)
list_of_files< - lapply(file_list,read.csv)
m1< ; - merge_all(list_of_files,by = c(Lat,Lon),all = TRUE)

发出错误

  merge.data.frame(dfs [[1]]中的错误,Recall(dfs [ - 1]),all = TRUE,sort = FALSE,
正式参数all由多个实际参数匹配

任何人都可以在这方面帮助我。

解决方案

您可以使用 减少 合并

  m1<  - 减少(function旧,新){merge(old,new,by = c ('Lat','Lon'))},list_of_files)


I have around 100 csv files with common headers, which i want to merge. The headers are "Lat", "Long" and "value". I am trying to merge all the csv files such that the output would be

"Lat" "Lon" "Value1" "Value2"..."Value 100" 

Lat and Lon columns are identical for all of the csv files. Merging two files is easy

merge(data.frame1, data.frame2, by=c('Lat','Lon'))

However, I tried the following code which didnt work:

file_list <- list.files(~/source)   
list_of_files <- lapply(file_list, read.csv)  
m1 <- merge_all(list_of_files, by=c("Lat","Lon"), all=TRUE)  

which throws the error

Error in merge.data.frame(dfs[[1]], Recall(dfs[-1]), all = TRUE, sort = FALSE,  : 
  formal argument "all" matched by multiple actual arguments.  

Can anyone help me in this regard.

解决方案

You can use Reduce and the plain merge:

m1 <- Reduce(function(old, new) { merge(old, new, by=c('Lat','Lon')) }, list_of_files)

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

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