merging multiple csv files in R using do.call

冷暖自知 提交于 2019-12-04 19:16:13

问题


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

回答1:


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

Use Reduce instead:

Reduce(function(x, y) merge(x, y, by="Species"), list(iris, iris, iris))
#works



回答2:


do.call accepts a list of all arguments, so try:

my.df <- do.call("merge",append(lapply(csvfiles, read.csv, header = TRUE), list(by=c("date","fx_code")) )


来源:https://stackoverflow.com/questions/22644780/merging-multiple-csv-files-in-r-using-do-call

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!