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
  • 789,025
  • 32
  • 460
  • 575
  • 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 – 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

2 Answers2

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
  • 789,025
  • 32
  • 460
  • 575
0

I used your advices with lapply, it looks really better:

funct_test<-function(variable)
  {
   path<-'C:/Users/ppachlinski/Documents/lekcja1_12c/smog_krakow/'
  my_data <- list.files(path)
  list_df <- lapply(my_data, read.table, sep = ",", header = TRUE)
  ?lapply
  mylist <- lapply(sciezka, read.table, header = TRUE, sep = ',') 

  mean(mylist[[1]]$variable, na.rm=TRUE)
  }

But I am facing problem with using this function: funct_test("X169_pressure")

I receive an error: In mean.default(mylist[[1]]$variable, na.rm = TRUE) : argument is not numeric or logical: returning NA

  • You can't use `$` with strings stored in objects. Use `[` instead. So replace `mylist[[1]]$variable` with `mylist[[1]][variable]`. – Gregor Thomas Mar 22 '20 at 20:39
  • Instead of trying to deal with the `path` separately from the filename, it is typically better to include the full path-to-filename as the filename, using `list.files(..., full.names=TRUE)` so that the entire directory path is included. This helps reproducibility by providing a more general approach in this function here, so that you don't have to change the function whenever you work on a newer version of the project. – r2evans Mar 22 '20 at 20:51