0

I have multiple files that I would like to essentially merge (.txt and .csv). They are all very different tables so I would essentially like to have about 30 sheets of different tables, and then be able to save that one file and index it later.

I've had trouble trying to find the most efficient way to do this, as most of my searches have ended up trying to merge() files together, which isn't possible since this collection of data files are unique.

The biggest issue is that each data frame is different, varying in names of columns and number of rows, unlike similar questions that have been asked.

What's the best way to combine the tables I have into one array, and save it?

EDIT: To add some more detail, I have essentially three different kinds of data frames from multiple different files:

.csv files with table headers "X" "gene" "baseMean" "log2FoldChange" "lfcSE" "stat"
"pvalue" "padj" "TuLob" "TuDu"

one kind of .txt files with headers "hgnc_symbol" "ensembl_gene_id" "ensembl_transcript_id" "ensembl_peptide_id"
"band" "chromosome_name" "start_position" "end_position"
"transcript_start" "transcript_end" "description" "go_id"
"name_1006" "transcript_source" "status"

and a second kind of .txt files with headers "hgnc_symbol" "ensembl_gene_id" "ensembl_transcript_id" "ensembl_peptide_id"
"band" "chromosome_name" "start_position" "end_position"
"transcript_start" "transcript_end" "description" "name_1006"
"transcript_source" "status"

Again, I'm not trying to merge these tables, just save them in a stack or three dimension array as one file, to be opened and indexed later

  • possible duplicate of [Write different data frame in one .csv file with R](http://stackoverflow.com/questions/17677557/write-different-data-frame-in-one-csv-file-with-r) – rmuc8 Mar 26 '15 at 14:43
  • I tried the method here, was unable to load the .csv back into R: "duplicate row names not allowed" – user2241427 Mar 26 '15 at 14:53
  • Well, when you write the data out, don't include the row names: `row.names=FALSE` – MrFlick Mar 26 '15 at 15:30

1 Answers1

0

I think what you want to do is use the save function to save the data in R's internal format.

df1 <- data.frame(x=rnorm(100))
df2 <- data.frame(y=rnorm(10), z=rnorm(10))

Gives us two data frames with different columns, rows, etc.

save(df1, df2, file="agg.RData")

Saves it to agg.RData

You can later do a

load("agg.RData")
head(df1)
...

See also attach, which does what load does, only lazily, so it will only load the objects once you try to access them.

Finally, you can get some measure of isolation by specifying and environment for load:

agg <- new.env()
load("agg.RData", agg)
head(agg$df1)
...
user295691
  • 7,108
  • 1
  • 26
  • 35