0

I have a code which I want to run on multiple ".RData" files (shown below)

     load("MYDAILYSALES.RData")
     load("AUDAILYSALES.RData")
     load("CNDAILYSALES.RData")
     load("SGDAILYSALES.RData")
     load("KRDAILYSALES.RData")
     load("AEDAILYSALES.RData")
     load("TWDAILYSALES.RData")
     load("JPDAILYSALES.RData")
     load("BDDAILYSALES.RData")
     load("VNDAILYSALES.RData")
     load("INDAILYSALES.RData")
     load("NZDAILYSALES.RData")
     load("IDDAILYSALES.RData")
     load("PHDAILYSALES.RData")
     load("THDAILYSALES.RData")
     load("HKDAILYSALES.RData")

I want to save the above a list or a vector or in some way, such that I can use it to reference a for(...) loop and traverse over multiple data sources.

rtaero
  • 31
  • 7
  • Possible duplicate of http://stackoverflow.com/questions/14757668/combine-multiple-rdata-files-containing-objects-with-the-same-name-into-one-sin – akrun Mar 30 '17 at 08:05
  • Also see [this post](http://stackoverflow.com/questions/17499013/how-do-i-make-a-list-of-data-frames). My answer there might be helpful depending on how you want to store the data. – lmo Mar 30 '17 at 13:21

1 Answers1

0

Put your list of sources into a vector:

data_sources <- c("1.Rdata", "2.Rdata" ...)

and loop over the resulting vector:

for (data in data_sources) {
   load(data)
}

And if you want to combine the resulting datasets into one:

save(list=ls(), file="all_together.Rdata")

This will save everything which is loaded in the environment, including all the data loaded from the list of Rdata files in the data_sources vector.

Parsa
  • 2,485
  • 2
  • 14
  • 30
  • c() doesn't work, but I wasnt aware of the save function. Thank You @par or that – rtaero Mar 30 '17 at 08:16
  • Sorry @par, I made a mistake. It works Perfectly. I want to save the output in different sheets of a spreadsheet. is there a way i can do this? I have 16 outputs and the "xlsx" package doesnt work in my laptop. – rtaero Mar 31 '17 at 01:49
  • better still I want to store the output in a data.frame – rtaero Mar 31 '17 at 02:57
  • how can I save the output as a list, if at the end of each loop I have a dataframe "devv" as 1 output. I need to store 50 dataframes (loop outputs) in the list. – rtaero Apr 04 '17 at 03:26
  • Do you want to save the output as a list, dataframe, or xlsx file? (By dataframe do you mean table or csv file that you can read in with 'read.csv()' or 'read.table()') – Parsa Apr 04 '17 at 07:41
  • Unfortunately the "xlsx" package doesn't work in my laptop and throws a Java error. I want to save it in anyway that I can eventually do a "write.csv" and save it – rtaero Apr 04 '17 at 17:34
  • @rtaero your question is very difficult to answer, an 'Rdata' file can store vectors, numbers, characters, dataframes, lists or a combination of any of these. So I have no idea what is in your Rdata file, what the variable names are or what the structure is. I think you would get more success here if you asked a separate question to this one with a bit more detail – Parsa Apr 05 '17 at 08:10