如何用循环绑定许多数据帧? [英] How to cbind many data frames with a loop?

查看:48
本文介绍了如何用循环绑定许多数据帧?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有105个带有xts的数据框,动物园类和II想将其第6列合并为一个数据框.

I have 105 data frames with xts, zoo class and II want to combine their 6th columns into a data frame.

因此,我创建了一个包含所有数据框名称的数据框,以将其与"for"功能配合使用:

So, I created a data frame that contains all the data frame names to use it with a 'for' function:

mydata <- AAL

for (i in 2:105) {
  k <- top100[i,1] # The first column contains all the data frame names
  mydata <- cbind(mydata, k)
}

这显然是错误的,但是我既不知道如何绑定这么多名称完全不同的数据帧(我的数据帧名称是NASDAQ Symbols),也不知道如何选择所有第六列.

It's obviously wrong, but I have no idea either how to cbind so many data frames with completely different names (my data frame names are NASDAQ Symbols) nor how to pick the 6th column of all.

提前谢谢

推荐答案

尝试foreach软件包.也许有一种更优雅的方法可以完成此任务,但是这种方法可以工作.

Try foreach package. May be there is more elegant way to do this task, but this approach will work.

    library(foreach)
    #create simple data frames with columns named 'A' and 'B'
    df1<-t(data.frame(1,2,3))
    df2<-t(data.frame(4,5,6))
    colnames(df1)<-c('A')
    colnames(df2)<-c('B')
    #make a list 
    dfs<-list(df1,df2)
    #join data frames column by column, this will preserve their names
    foreach(x=1:2
            ,.combine=cbind)%do% # don`t forget this directive
    {
      dfs[[x]]
    }

结果将是:

       A B
    X1 1 4
    X2 2 5
    X3 3 6

选择第6列:

df[,6]

这篇关于如何用循环绑定许多数据帧?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆