将多个数据帧存储到一个数据结构中 [英] Storing multiple data frames into one data structure - 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))
..然后我希望将它们添加到数据结构中,以便我可以循环访问每个数据帧的数据结构,并一次处理它, / p>
.. 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?
推荐答案
只要把 data.frames
在列表中。一个加号是,列表
与应用
样式循环非常相似。例如,如果要保存data.frame,可以使用 mapply
:
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"))
如果你喜欢应用
样式循环(你会相信我: ))请看看史诗般的 plyr
包。它可能不是最快的包(快速查找 data.table
),但它会与语法糖滴下。
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.
这篇关于将多个数据帧存储到一个数据结构中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!