使用do.call合并R中的多个csv文件 [英] merging multiple csv files in R using do.call
本文介绍了使用do.call合并R中的多个csv文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用以下代码导入和合并一组csv文件,但似乎没有将by = c("X","Y")参数传递给合并函数.有关如何解决此问题的任何建议?谢谢
I am trying to import and merge a set of csv files using the following code, but it doesn't seem to be passing the by=c("X","Y") argument to the merge function. Any recommendations on how to fix this? Thank you
csvfiles <- list.files(path = ".", pattern='bbg_.*\\.csv$')
csvfiles <- paste("fx/",csvfiles,sep="")
csvfiles
my.df <- do.call("merge",list(lapply(csvfiles, read.csv, header = TRUE), by=c("date","fx_code")))
推荐答案
merge
不接受超过2个data.frame,因此您不能使用do.call
将其传递给更大的列表:
merge
doesn't accept more than 2 data.frames, so you can't pass it a larger list using do.call
:
do.call(merge, list(iris, iris, iris))
#Error in fix.by(by.x, x) :
# 'by' must specify one or more columns as numbers, names or logical
改为使用Reduce
:
Reduce(function(x, y) merge(x, y, by="Species"), list(iris, iris, iris))
#works
这篇关于使用do.call合并R中的多个csv文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文