Skip to content

Instantly share code, notes, and snippets.

@MrFlick
Last active August 29, 2015 14:00
Show Gist options
  • Save MrFlick/11407464 to your computer and use it in GitHub Desktop.
Save MrFlick/11407464 to your computer and use it in GitHub Desktop.
read.stack.R: reads a list of files into a single data.frame and allows you to add extra columns to each
read.stack <- function(files, ..., select=NULL, extra=NULL, reader=read.table) {
dd<-data.frame()
if(!is.null(extra)) {
stopifnot(is.list(extra))
stopifnot(all(sapply(extra, length)==length(files)))
}
for(i in 1:length(files)) {
d<-reader(files[i], ...)
if(!is.null(select)) {
stopifnot(all(select %in% names(d)))
d<-d[, select]
}
if(!is.null(extra)) {
d<-do.call(cbind, c(list(d), lapply(extra, '[', i)))
}
if(nrow(dd)>0) {
dd<-rbind(dd, d)
} else {
dd<-d
}
}
dd
}
#paste0 <- function(...) paste(..., sep="")
regions <- c("exome","chr20")
conditions <- c("exp1","exp2","exp3")
opts <- expand.grid(cond=conditions, region=regions)
dd <- read.stack(paste0("comp.", opts$region, "/", opts$cond, ".txt"), header=T, extra=opts)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment