Skip to content

Instantly share code, notes, and snippets.

@codegordi
Created May 31, 2013 17:51
Show Gist options
  • Save codegordi/5686676 to your computer and use it in GitHub Desktop.
Save codegordi/5686676 to your computer and use it in GitHub Desktop.
Read to / write from list - from / to flat file
# use plyr package
library(plyr)
### READ all files in working directory into a list of dataframes
d.list=lapply(list.files(getwd(), full = F), FUN = read.table, header=T, sep="\t", fill=T) # name data
### SEPARATE out data frames from list -- using a for-loop in this example
i=0
f.coll=list.files()
for (f in f.coll) {
i=i+1;
print(f); print(i) # "progress indicator"
f.name=paste(unlist(str_split(f, ".txt"))[1], "dataset-suffix", sep=".")
assign(f.name, as.data.frame(d.list[i]))
}
### WRITE all dataframes in list to tab-delimited flat file
l_ply(1:length(d.list), function(x, d=d.list[[x]])
write.table(d, file = paste(d$label[2], "txt", sep = "."), sep = "\t", row.names = FALSE)) # name file after variable "label"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment