r - Read and rbind multiple csv files -
i have series of csv files (one per anum) same column headers , different number of rows. reading them in , merging them so;
setwd <- ("n:/ring data cruise/shetland") lengthheight2013 <- read.csv("n:/ring data cruise/shetland/r_0113a_s2013_wd.csv",sep=",",header=true) lengthheight2012 <- read.csv("n:/ring data cruise/shetland/r_0212a_s2012_wd.csv",sep=",",header=true) lengthheight2011 <- read.csv("n:/ring data cruise/shetland/r_0211a_s2011_wod.csv",sep=",",header=true) lengthheight2010 <- read.csv("n:/ring data cruise/shetland/r_0310a_s2010_wod.csv",sep=",",header=true) lengthheight2009 <- read.csv("n:/ring data 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 know if there shorter/tidier way this, considering each time run script might want @ different range of years.
i found bit of code tony cookson looks want, data frame produces me has correct headers 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
) , read files in loop (lapply
), call (do.call
) row bind (rbind
) put files rows.
mymergeddata <- do.call(rbind, lapply(list.files(path = "n:/ring data cruise"), read.csv))
Comments
Post a Comment