Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Storing multiple data frames into one data structure - R

Tags:

dataframe

r

Is it possible to have multiple data frames to be stored into one data structure and process it later by each data frame? i.e. example

df1 <- data.frame(c(1,2,3), c(4,5,6))
df2 <- data.frame(c(11,22,33), c(44,55,66))

.. then I would like to have them added in a data structure, such that I can loop through that data structure retrieving each data frame one at a time and process it, something like

 for ( iterate through the data structure) # this gives df1, then df2
 {
    write data frame to a file
}

I cannot find any such data structure in R. Can anyone point me to any code that illustrates the same functionality?

like image 742
name_masked Avatar asked Sep 04 '12 17:09

name_masked


People also ask

How do I combine multiple data frames into one in R?

To join two data frames (datasets) vertically, use the rbind function. The two data frames must have the same variables, but they do not have to be in the same order. If data frameA has variables that data frameB does not, then either: Delete the extra variables in data frameA or.

How do I combine three data frames in R?

Join Multiple R DataFrames To join more than two (multiple) R dataframes, then reduce() is used. It is available in the tidyverse package which will convert all the dataframes to a list and join the dataframes based on the column.

How do I merge two data frames in the same column in R?

To combine two data frames with same columns in R language, call rbind() function, and pass the two data frames, as arguments. rbind() function returns the resulting data frame created from concatenating the given two data frames. For rbind() function to combine the given data frames, the column names must match.

Can you merge more than 2 datasets in R?

The merge function in R allows you to combine two data frames, much like the join function that is used in SQL to combine data tables. Merge , however, does not allow for more than two data frames to be joined at once, requiring several lines of code to join multiple data frames.

What is a Dataframe in R?

Data frames are very similar to how most people are used to seeing data represented, in a table of rows and columns. The data frame in R is composed of columns of vectors. Data frames are ideal for plotting data and are used as inputs for linear and logistic regression, the K-nearest neighbors algorithm.

How to merge multiple data frames in R?

You can use one of the following two methods to merge multiple data frames in R: #put all data frames into list df_list <- list (df1, df2, df3) #merge all data frames in list Reduce (function (x, y) merge (x, y, all=TRUE), df_list)

What is a data structure in R?

A data structure is a particular way of organizing data in a computer so that it can be used effectively. The idea is to reduce the space and time complexities of different tasks. Data structures in R programming are tools for holding multiple values.

How to split a data frame into several smaller data frames?

You can use one of the following three methods to split a data frame into several smaller data frames in R: #define first n rows to include in first data frame n <- 4 #split data frame into two smaller data frames df1 <- df [row.names(df) %in% 1:n, ] df2 <- df [row.names(df) %in% (n+1):nrow (df), ]


2 Answers

Just put the data.frames in a list. A plus is that a list works really well with apply style loops. For example, if you want to save the data.frame's, you can use mapply:

l = list(df1, df2)
mapply(write.table, x = l, file = c("df1.txt", "df2.txt"))

If you like apply style loops (and you will, trust me :)) please take a look at the epic plyr package. It might not be the fastest package (look data.table for fast), but it drips with syntactic sugar.

like image 137
Paul Hiemstra Avatar answered Oct 17 '22 10:10

Paul Hiemstra


Lists can be used to hold almost anything, including data.frames:

## Versatility of lists
l <- list(file(), new.env(), data.frame(a=1:4))

For writing out multiple data objects stored in a list, lapply() is your friend:

ll <- list(df1=df1, df2=df2)
## Write out as *.csv files
lapply(names(ll), function(X) write.csv(ll[[X]], file=paste0(X, ".csv")))
## Save in *.Rdata files
lapply(names(ll), function(X) {
    assign(X, ll[[X]]) 
    save(list=X, file=paste0(X, ".Rdata"))
})
like image 33
Josh O'Brien Avatar answered Oct 17 '22 10:10

Josh O'Brien