Last active
August 29, 2015 14:00
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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