代码之家  ›  专栏  ›  技术社区  ›  Laura

使用dplyr从其他数据帧创建新的数据帧

  •  1
  • Laura  · 技术社区  · 6 年前

        df.1<-as.data.frame(matrix(rnorm(10),250,149))
        df.2<-as.data.frame(matrix(rnorm(10),250,149))
        df.3<-as.data.frame(matrix(rnorm(10),250,149))
    

    我想用以下条件创建另一个数据帧:

    the columns 1,4,7,10,...445 of df.4... will be the columns from df.1
    
    the columns 2,5,8,11,...446 of df.4... will be the columns from df.2
    
    the columns 3,6,9,12,...447 of df.4... will be the columns from df.3
    

    在ende我的df.4将会有 3*149 柱。

    我该怎么做呢 dplyr

    最重要的是 ,我怎样才能记住我的专栏的名字???

    1 回复  |  直到 6 年前
        1
  •  1
  •   Abdallah Atef    6 年前

    为此,我们将数据帧与 bind_cols() 然后重新订购 select() 具体如下:

    library (dplyr)
    df.4 <- bind_cols(df.1, df.2, df.3)
    # create the column order 
    order <- c()
    for(i in 1:149){
        temp <- c(i, i+149, i+298)
        order <- c(order, temp)
    }
    df.4 <- df.4 %>% select(order)
    

    这里使用的循环是在数字1:149上循环,无论数据大小都不会变慢