0

I have a number of dataframe variables like store_1, store_2, store_3...... I want to keep the variables name in a list, like store=[store_1, store_2, store_3,....] so that when ever I do store[1] or store[2] i get the variables store_1 or store_2 and then i can use these variables to get some values as they are datarame variables. But I am getting error when I am trying to do it. I have give a simple example below to make my wordings more clear

group_by_week <- data.frame(Year=integer())
group_by_week[1,1]=5
group_by_week[1,1] # on executing this i get 

> group_by_week[1,1]
[1] 5

Now I am creating a list and entering the dataframe name group_by_week into it

storeList = list()
storeList = append(storeList,'group_by_week')
storeList[1] # on executing this I am getting

> storeList[1]
  [[1]]
  [1] "group_by_week"

But now if i try to do storeList[1][1,1] I am getting error Error in storeList[1][1, 1] : incorrect number of dimensions which i think is obvious to get but don't know how to solve it.

tanay
  • 438
  • 5
  • 15
  • 1
    You can use `store – akrun Apr 18 '16 at 02:29
  • Ya If I do storeList[1] then i am getting "group_by_week" but I am not able to access the dataframe with the name group_by_week – tanay Apr 18 '16 at 02:35
  • 1
    I am not sure what you wanted. Perhaps `store – akrun Apr 18 '16 at 02:39
  • My intention is that I have a bunch of dataframes and I have stored them in a list. Now if I want to access a record in a dataframe then how I am supposed to do that... First I have to get the dataframe name from the list and do what is required. I am able to get the dataframe name from the list but then I am unable to access that dataframe. – tanay Apr 18 '16 at 03:06
  • Why would you want just the names in a list? Just put the data frames in a list so `store[[1]]` **is** what you currently have as `store_1`. Then you don't have to mess with `get`, `mget`, `assign`, etc. See [How do I make a list of data frames](http://stackoverflow.com/a/24376207/903061) for plenty of examples. – Gregor Thomas Apr 18 '16 at 03:07
  • 2
    As to "*Now if I want to access a record in a dataframe then how I am supposed to do that...*", if `store[[1]]` is a data frame, then use it like a data frame! You can do `store[[1]][3, ]` for the 3rd row, `store[[1]]$col_name`, etc. – Gregor Thomas Apr 18 '16 at 03:08
  • I'm very tempted to mark as duplicate of [How do I make a list of data frames](http://stackoverflow.com/a/24376207/903061). Is there anything not there that you're confused about? – Gregor Thomas Apr 18 '16 at 03:10
  • 1
    The problem in `append(storeList,'group_by_week')` is that `'group_by_week'` is just a string. As the other commenters are suggesting you could do use `mget('group_by_week')` if you need to use a string column name. If you don't need a string column name then `append(storeList, group_by_week)` works. – Gregor Thomas Apr 18 '16 at 03:13
  • 2
    You might also like [What is the difference between `[[` and `[` for accessing elements of a list?](http://stackoverflow.com/q/1169456/903061). – Gregor Thomas Apr 18 '16 at 03:14
  • I got it... Thanks for guiding me through... – tanay Apr 18 '16 at 03:22

0 Answers0