user1675522
user1675522

Reputation: 9

R - Storing multiple data frames into an array

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.

Upvotes: -1

Views: 3421

Answers (1)

ctbrown
ctbrown

Reputation: 2361

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.

Upvotes: 4

Related Questions