0

Could you explain me what I am doing wrong here? Issue seems to be very basic so I dont use reproducible example. Here is my code:

#first i load certain data bases in csv from my path
list.df_names=list.files("D:/my_path",pattern = ".csv")

The output of list.df_names:

[1] "df1.csv"  "df2.csv" "df3.csv"

Now what is my main issue:

list.df=list(get(list.df_names))
length(list.df)

output:

[1] 1

And length should be 3 because there are three data frames in my list. When I use this code(after loading dfs to R):

list.df.good=list(df1.csv,df2,csv,df3,csv)
lenght(list.df.good)

output

[1] 3

What am I wrong here? Writing all df names only to put them into list is really inconvenient in long run.

Thank you in advance.

EDIT1: I missed one importan information: I loaded all dfs from my list.df.names INTO R, so i have objects(data frames) named like this: df1.csv, df2.csv, df3.csv. They are separate data frames and I want to put its content into one list.

Alexandros
  • 301
  • 1
  • 10
  • I think you have possibly forgotten some quotes (near `get` and `list.df.good = ...`). Can you edit the question please... THX! – R Yoda Nov 21 '17 at 17:28
  • Why do you use `get`? And why do you want to store strings in a list instead of an vector? Understanding your use case would help us to find a better solution – R Yoda Nov 21 '17 at 17:36
  • You have to read data in to R using `read.csv` (or similar). You can't just jump from filename to R object. Try `list.df = lapply(list.df_names, read.csv)`. – Gregor Thomas Nov 21 '17 at 17:40
  • 2
    Suggested duplicate: [How to make a list of data frames](https://stackoverflow.com/a/24376207/903061). My answer there covers reading data frames from files. – Gregor Thomas Nov 21 '17 at 17:40
  • I edited my post. My dfs are already in R i simply want to use list of its names tell R which dfs to put into list. Their names are same as file names. Sorry for that confusion. – Alexandros Nov 21 '17 at 18:09
  • In that case it is certainly a duplicate. Your only issue is that you are using `get` when you need `mget`. `get` only works on one object, `mget` is vectorized. – Gregor Thomas Nov 21 '17 at 18:29
  • There was different issue with my code I didnt clearly covered in my post. As soon as I got familiar with your link and checked again my full code i figured out what was wrong: `list.df=list(get(list.df_names))` has to be `list.df=get(list.df_names)` or `mget`. Thank you for your help. – Alexandros Nov 22 '17 at 11:57

0 Answers0