Reputation: 10822
Lets say I have 2 data tables:
data_table1
data_table2
I want to be able to store them in a structure and with keys pointing to them so that I can easily extract them as needed.
'data_table1' is the key for datatable1
for e.g.
How do I do this in R and what structure should I use?
Upvotes: 1
Views: 65
Reputation: 2375
To elaborate on ilir's suggestion with an example, if you have two differently structured data.frames, you can use a list like so:
df1 <- data.frame(id = 1:5, data = runif(5))
df2 <- data.frame(id = 1:3, data = sample(letters, 3))
alldata <- list(data_table1 = df1, data_table2 = df2)
alldata$data_table1 # access df1
alldata$data_table2 # access df2
If your data frames have the same structure, you could combine them for example with an extra key column (here table
) like so:
df1 <- data.frame(table = "data_table1", id = 1:5, data = runif(5))
df2 <- data.frame(table = "data_table2", id = 1:3, data = runif(3))
alldata <- rbind(df1, df2)
subset(alldata, table == "data_table1") # access df1
subset(alldata, table == "data_table2") # access df2
Upvotes: 1