0

I have 20 csv files which I need to upload I made in a loop and then I added each data.frame to a vector. Finally in the vector "list_df" I have 20 elements where I stored name of my 20 dataframes.

Now I am trying to get to those dateframe stored in list_df but it doesn`t work. Any ideas how can I get to those data frames stored in the vector to make futher calculation?

list_df[1][column_name] 

or

list_df[1]$column_name

doesn`t work

path<-'thats my path'

list_of_files<-list.files(path) 

list_df<-c() #creating empty vector
for (i in 1:length(list_of_files)){
  assign(paste("dffile",list_of_files[i],sep=""),(read.table(paste(path,list_of_files[i],sep=""), sep=",", header=TRUE)))
  list_df[i]<-paste("dffile",list_of_files[i],sep="")
}
akrun
  • 874,273
  • 37
  • 540
  • 662
  • 2
    Gawel, you are going down the road of some very bad (in R) practices: (1) use of `assign`; and (2) growing objects ([R Inferno](https://www.burns-stat.com/pages/Tutor/R_inferno.pdf) chapter 2). There is the potential here for really confusing variables and names of variables, but I'm not certain since we don't know what these things look like. I suggest you read *R Inferno* chapter 2, as well as an answer about dealing with [lists of frames](https://stackoverflow.com/a/24376207/3358272) instead of the `assign` method of creating lots of (similar) objects. – r2evans Mar 22 '20 at 18:45
  • BTW: you aren't storing a frame within the list, you are storing a string (or vector of strings) that is returned by `paste`. If you do `str(list_df)`, I suspect you'll see `"dffilesomefile.csv"` (and more). Perhaps replace all of your `for` loop with `list_df <- lapply(list_of_files, read.table, sep = ",", header = TRUE)`. – r2evans Mar 22 '20 at 18:55
  • You are right @r2evans. I have the list of strings. My question is how I could transform those string value which contains dataframe`s name. – Gaweł Chiński Mar 22 '20 at 19:01
  • I'll reiterate that using `assign` and `get` should really be discouraged. If your files are all structured about the same, then keeping them in a list helps: when you do something to one of them, using `lapply` you can do the same thing to all of them just as easily (no `for` loop required). Similarly, the use of `assign` and `get` can be difficult for reproducibility. But if you're still insistent on using `assign`, look at `get` and `mget`. – r2evans Mar 22 '20 at 19:56
  • 1
    Thx r2evans. I used your method with lapply and it really looks better. Thx a lot. – Gaweł Chiński Mar 22 '20 at 20:24

1 Answers1

1

We can initialize list_df as a character vector

list_df <- character(length(list_of_files))

Now, the index based assignment should work.


As 'list_df' contains the object names as a string, if we need to get the values of those elements, use get (for single object) or mget (for all objects in a list)

get(list_df[1])
mget(list_df)
akrun
  • 874,273
  • 37
  • 540
  • 662