简体   繁体   English

如何在r中加载和合并多个.csv文件?

[英]How to load and merge multiple .csv files in r?

So I'm very new at R and right now I'm trying to load multiple .csv files (~60 or so) and then merge them together. 所以我在R上很新,现在我正在尝试加载多个.csv文件(~60左右),然后将它们合并在一起。 They all have similar columns and their files are named like this: dem_file_30, dem_file_31. 它们都有类似的列,它们的文件命名如下:dem_file_30,dem_file_31。

I've been trying to use scripts online but keep getting some errors. 我一直在尝试在线使用脚本但是仍然遇到一些错误。 I'm sure I can do it by hand but that would be really tedious. 我相信我可以手工完成,但那真的很乏味。

Example: 例:

file_list <- list.files("/home/sjclark/demographics/")   
list_of_files <- lapply(file_list, read.csv)  
m1 <- merge_all(list_of_files, all=TRUE)
Error: merge_all doesn't exist

This one seems to read them into R, but then I'm not how to do after that... help? 这个似乎把它们读成了R,但后来我不知道怎么办......帮忙?

setwd("/home/sjclark/demographics/")
filenames <- list.files(full.names=TRUE)  
All <- lapply(filenames,function(i){
read.csv(i, header=TRUE)
})

It appears as if you might be trying to use the nice function shared on R-bloggers (credit to Tony Cookson) : 看起来好像你可能正在尝试使用R-bloggers上共享的nice函数(归功于Tony Cookson)

multMerge = function(mypath){
  filenames = list.files(path = mypath, full.names = TRUE)
  datalist = lapply(filenames, 
                    function(x){read.csv(file = x,
                                         header = TRUE,
                                         stringsAsFactors = FALSE)})
  Reduce(function(x,y) {merge(x, y, all = TRUE)}, datalist)
}

Or perhaps you have pieced things together from difference sources? 或者也许你已经从差异来源拼凑了一些东西? In any event, merge is the crucial base R function that you were missing. 无论如何, merge是你缺少的关键基础R函数。 merge_all doesn't exist in any package. merge_all在任何包中都不存在。

Since you're new to R (and maybe all programming) it's worth noting that you'll need to define this function before you use it. 由于您是R(也许是所有编程人员)的新手,因此值得注意的是,在使用它之前,您需要定义此函数。 Once you've done that you can call it like any other function: 完成后,您可以像任何其他功能一样调用它:

my_data <- multMerge("/home/sjclark/demographics/")

I have just been doing a very similar task and was also wondering if there is a faster/better way to do it using dplyr and bind_rows . 我刚刚做了一个非常相似的任务,也想知道是否有更快/更好的方法使用dplyrbind_rows

My code for this task is uses ldply from plyr : 我执行此任务的代码是使用ldplyplyr

library(plyr)    
filenames <- list.files(path = "mypath", pattern = "*", full.names=TRUE)
import.list <- ldply(filenames, read.csv)

Hope that helps 希望有所帮助

Rory 罗里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM