0

I'm new to R and was wondering if i can store multiple data frames in a vector.

Example: If i have 2 data frames:

df1 <- data.frame(CustomerId=c(1:6),Product=c(rep("Toaster",3),rep("Radio",3)))  

df2 <- data.frame(CustomerId=c(2,4,6),State=c(rep("Alabama",2),rep("Ohio",1)))  

df1

  CustomerId Product  
1          1 Toaster  
2          2 Toaster  
3          3 Toaster  
4          4   Radio  
5          5   Radio  
6          6   Radio

df2

  CustomerId   State  
1          2 Alabama  
2          4 Alabama  
3          6    Ohio  

I want store these 2 data frames in a single array df such that if I enter >df[1] I would get df1 and if I enter >df[2] I would get df2.

I want to know if this is possible or even any alternate solution would be great.

josliber
  • 41,865
  • 12
  • 88
  • 126
  • possible duplicate of [How do I make a list of data frames in r](http://stackoverflow.com/questions/17499013/how-do-i-make-a-list-of-data-frames-in-r) – rmuc8 Apr 21 '15 at 12:55
  • I suppose you did not find the previous answer because you were looking for an array instead of a list. But if you try to store the data.frames in an array via `df – rmuc8 Apr 21 '15 at 13:01
  • 1
    I would go with `mget(ls(pattern = "df"))` if you have many `df`s in the global envoronment – David Arenburg Apr 21 '15 at 13:04

1 Answers1

4

You should use a list:

list( df1, df2 )  # -or-
list( df1=df1, df2=df2 ) 

I would be surprised if this isn't already answered somewhere on SO.

ctbrown
  • 2,121
  • 14
  • 24