Created
May 31, 2013 17:51
-
-
Save codegordi/5686676 to your computer and use it in GitHub Desktop.
Read to / write from list - from / to flat file
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
# 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