R - use rbind on multiple variables with similar names

前端 未结 2 2001
离开以前
离开以前 2020-11-28 13:06

I have many variables that I have created using code like this:

for (i in 1:10) {
    assign(paste0(\"variable\", i), i )}

I now need to us

2条回答
  •  盖世英雄少女心
    2020-11-28 13:48

    I would like to add another way to merge multiple dataframes with dynamic names. This will be accomplished by using mget and bind_rows from dplyr.

    # 3 Data frames are created
    TXN_MONTH_01 <- data.frame(a = 1:10, b = 101:110)
    
    TXN_MONTH_02 <- data.frame(a = 11:20, b = 111:120)
    
    TXN_MONTH_03 <- data.frame(a = 21:30, b = 121:130)
    
    #create a list using dynamic names of dataframes
    z <- as.list(mget(paste("TXN_MONTH_0", 1:3, sep="")))
    library(dplyr)
    #now call bind rows
    bind_rows(z)
    #    a   b
    #1   1 101
    #2   2 102
    #3   3 103
    #4   4 104
    #5   5 105
    #.....
    #.....
    #25 25 125
    #26 26 126
    #27 27 127
    #28 28 128
    #29 29 129
    #30 30 130
    

提交回复
热议问题