0

I need specific files from "global environment" to run some calculations for each file

I have 390 data files with nearly the same structure in the global environment and i need to take 330 specific files. So I ran a for-loop to compare the elements from the global environment with a list that contains the elements names I need to take. For each file I need to transform a factor variable into dummy-coding.

The for-loop is the following:

for(x in t(upc.unique))
 {     
 xName <- paste(x,"csv", sep = ".")
   for(y in tmp)          #tmp equals the elements of the global environment
   {   
   as.character(tmp)
   if (xName == y)      
    { as.list(tmp)         
    assign(tmp[i], paste(tmp[i],"d")<-cbind(tmp[i], model.matrix(~factor(tmp[i]$SALES) - 1))) 

    }
   }

 }

"upc.unique" contains the names of the data files I need to pick from the global environment "tmp" is a large list of each element of the "global environment"

My question is if there's a better and faster way to do the task. Im pretty sure that there is a way, but because I am new with R, it is hard for me to find it.

zx8754
  • 52,746
  • 12
  • 114
  • 209
  • 2
    How is R-Studio relevant here? Also, see [this](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – David Arenburg Jul 06 '17 at 10:27
  • save.image("blah.RData") can save all the objects of your session into a binary file. Alternatively you can `saveRDS` each object – amonk Jul 06 '17 at 10:36
  • Are you sure you meant Global environment, instead of working directory? The global environment implies you already loaded the files, which makes the question as to how to load them pointless. – JAD Jul 06 '17 at 10:41
  • @JarkoDubbeldam I already loaded the files and maybe i wasn't clear with the question. Instead of loading them, I need R to choose exactly those files that are named in "upc.unique". And after that, I need to run the transformation from factor to dummy-coding. – Maximilian Jul 06 '17 at 10:46
  • So why not load all files in upc.unique, instead of ALL files? – JAD Jul 06 '17 at 10:48
  • 1
    So what you mean is: 1. Save all objects of my session (as @amonk said). 2. Load all elements from upc.unique into the (empty) glob.env. 3.Execute the calculation for the whole glob.env. ? I am going to try this! Thank you for your help, I wasnt clear about the fact that I can clear the whole environment without loosing the data I already created (as I am new to R). – Maximilian Jul 06 '17 at 10:52
  • @Maximilian yup. seems so. `Keep It Simple Solution` I guess – amonk Jul 06 '17 at 11:21

0 Answers0