0

I have written an R Script which will download a zip file from a certain location, and extract it to a specified folder in my Desktop using this solution from stackoverflow - Automate zip file reading in R

I have created a vector which contains the full file names as shown below.

File_Names
[1] "file1.csv"
[2] "file2.csv"

I am also able to read these files as separate dataframes into R using a for loop. Since I do not know how many files the downloaded zip file contains, my program reads all files which are present in the File_Names vector. How do I call these dataframes just by using their names using the File_Names vector? As example,

df <- File_Names[1] # df should have the same content as file1.csv, similar to copying one dataframe to another

rm (File_Names[2]) # when I run this, the dataframe which was read in to R using file2.csv should be removed from R memory

I want to use the names from File_Names to perform appropriate tasks in R. How do I go about achieving this?

Code_Sipra
  • 1,571
  • 4
  • 19
  • 38
  • Use `lapply` instead of `for` to get the data frames into a list, e.g. `lst`. (Thing of it as a bag of data frames in your case.) You can name the list elements according to your filenames, e.g. `names(lst)<-File_Names`. Then picking the right data frame is as easy as `lst[[File_Names[1]]]`. – lukeA Oct 18 '17 at 19:09
  • Thanks a lot for the quick reply, lukeA. I just have one question. Some of the files are huge (~500 mb). So if I convert them into a list, do you think this will have any impact on performance when processing the data frames? – Code_Sipra Oct 18 '17 at 19:15

1 Answers1

0

I know you asked 3 years ago, but for all out there: I had the same problem and just found a solution: You can call a dataframe or any R object with:

data.use <- get("nameofobject")
pbraeutigm
  • 455
  • 4
  • 8