阅读并rbind多个csv文件 [英] Read and rbind multiple csv files

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

问题描述

我有一系列csv文件(每一个一个)具有相同的列标题和不同的行数。原来我正在读他们,并合并他们像这样;

  setwd<  - (N:/通过巡航/设得兰格数据)
LengthHeight2013< - .csv(N:/通过巡航/ Shetland / R_0113A_S2013_WD.csv振铃数据,sep =,,header = TRUE)
LengthHeight2012< - read.csv(N: Shetland / R_0212A_S2012_WD.csv,sep =,,header = TRUE)
LengthHeight2011< - read.csv(N:/通过巡航/ Shetland / R_0211A_S2011_WOD.csv振铃数据,sep =, ,header = TRUE)
LengthHeight2010< - read.csv(N:/通过巡航环境数据/ Shetland / R_0310A_S2010_WOD.csv,sep =,,header = TRUE)
LengthHeight2009& - read.csv(N:/通过巡航/ Shetland / R_0309A_S2009_WOD.csv振铃数据,sep =,,header = TRUE)

LengthHeight< - merge(LengthHeight2013,LengthHeight2012,all = TRUE)
LengthHeight< - merge(LengthHeight,LengthHeight2011,all = TRUE)
LengthHeight< - merge(LengthHeight,LengthHeight2010,all = TRUE)
LengthHeight< - merge ,LengthHeight2009,all = TRUE)

我想知道re是一个更短或更简单的方法来做到这一点,也考虑到每次我运行脚本,我可能想看看不同的几年。



我也发现托尼·库克森(Tony Cookson)的这段代码看起来像我想要的,但是它为我生成的数据帧只有正确的头,但没有数据行。

  multmerge = function(mypath){
filenames = list.files(path = mypath,full.names = TRUE )
datalist = lapply(filenames,function(x){read.csv(file = x,header = T)})
减少(function(x,y){merge(x,y)} ,datalist)

mymergeddata = multmerge(C:// R // mergeme)


解决方案

查找文件( list.files ),并循环读取文件( lapply ),然后调用( do.call )行绑定( rbind )将所有文件一起排列。

  myMergedData<  -  
do.call(rbind,
lapply .files(path =N:/通过巡航记录数据),read.csv)


I have a series of csv files (one per anum) with the same column headers and different number of rows. Originally I was reading them in and merging them like so;

setwd <- ("N:/Ring data by cruise/Shetland")
LengthHeight2013 <- read.csv("N:/Ring data by      cruise/Shetland/R_0113A_S2013_WD.csv",sep=",",header=TRUE)
LengthHeight2012 <- read.csv("N:/Ring data by cruise/Shetland/R_0212A_S2012_WD.csv",sep=",",header=TRUE)
LengthHeight2011 <- read.csv("N:/Ring data by cruise/Shetland/R_0211A_S2011_WOD.csv",sep=",",header=TRUE)
LengthHeight2010 <- read.csv("N:/Ring data by cruise/Shetland/R_0310A_S2010_WOD.csv",sep=",",header=TRUE)
LengthHeight2009 <- read.csv("N:/Ring data by cruise/Shetland/R_0309A_S2009_WOD.csv",sep=",",header=TRUE)

LengthHeight <- merge(LengthHeight2013,LengthHeight2012,all=TRUE)
LengthHeight <- merge(LengthHeight,LengthHeight2011,all=TRUE)
LengthHeight <- merge(LengthHeight,LengthHeight2010,all=TRUE)
LengthHeight <- merge(LengthHeight,LengthHeight2009,all=TRUE)

I would like to know if there is a shorter/tidier way to do this, also considering that each time I run the script I might want to look at a different range of years.

I also found this bit of code by Tony Cookson which looks like it would do what I want, however the data frame it produces for me has only the correct headers but no data rows.

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

mymergeddata = multmerge("C://R//mergeme")

解决方案

Find files (list.files) and read the files in a loop (lapply), then call (do.call) row bind (rbind) to put all files together by rows.

myMergedData <- 
  do.call(rbind,
          lapply(list.files(path = "N:/Ring data by cruise"), read.csv))

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

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