0

I am performing a set of analyses in R. The flow of the analysis is reading in a dataframe (i.e. input_dataframe), performing a set of calculations that then result in a new, smaller dataframe (called final_result). A set of exact calculations is performed on 23 different files, each of which contains a dataframe.

My question is as follows: For each file that is read in (i.e. the 23 files) I am trying to save a unique R object: How do I do so? When I save the resulting final_result dataframe (using save() to an R object, I then cannot read all 23 objects into a new R session without having the different R objects override each other. Other suggestions (such as Create a variable name with "paste" in R?) did not work for me, since they rely on the fact that once the new variable name is assigned, you then call that new variable by its name, which I cannot do in this case.

To Summarize/Reword: Is there a way to save an object in R but change the name of the object for when it will be loaded later? For example:

x=5
magicSave(x,file="saved_variable_1.r",to_save_as="result_1")
x=93
magicSave(x,file="saved_variable_2.r",to_save_as="result_2")
load(saved_variable_1)
load(saved_variable_2)
result_1
#returns 5
result_2
#returns 93
Josh
  • 1,155
  • 4
  • 12
  • 21

2 Answers2

0
x=5
write.csv(x,"one_thing.csv", row.names = F)
x=93
write.csv(x,"two_thing.csv", row.names = F)
result_1 <- read.csv("one_thing.csv")
result_2 <- read.csv("two_thing.csv")

result_1
# x
# 1 5
result_2
# x
# 1 93
AidanGawronski
  • 2,055
  • 1
  • 14
  • 24
  • This solution will not allow for writing and reading dataframes or other complex data structures. Furthermore, it relies on the knowing the name of the object that I want to read to (i.e. `result_1 <- read.csv("one_thing.csv")` and assigning it to that object. What if I am saving 100 objects? – Josh Feb 28 '18 at 00:36
0

In R it's generally a good idea to actually store as a list everything that can be seen as a list. It will make everything more elegant afterwards.

First you put all your paths in a list or a vector :

paths <- c("C:/somewhere/file1.csv",
           "C:/somewhere/file2.csv") # etc

Then you read them :

objects <- lapply(paths,read.csv) # objects is a list of tables

Then you apply your transformation on each element :

output <- lapply(objects,transformation_function)

And then you can save your output (I find saveRDS cleaner than save as you know what variables you'll be inviting in your workspace when loading) :

saveRDS(output,"C:/somewhere/output.RDS")

which you will load with

output <- readRDS("C:/somewhere/output.RDS")

OR if you prefer for some reason to save as different objects:

output_paths <- paste0("C:/somewhere/output",seq_along(output),".csv")
Map(saveRDS,output,output_paths)

To load later with:

output <- lapply(paths, readRDS)
moodymudskipper
  • 46,417
  • 11
  • 121
  • 167
  • I think this is the best that is reasonably possible in R, so I am going to accept it. – Josh Feb 28 '18 at 15:48