rbind dataframes with a different column name

前端 未结 5 516
伪装坚强ぢ
伪装坚强ぢ 2020-11-27 20:28

I\'ve 12 data frames, each one contains 6 columns: 5 have the same name, 1 is different. Then when I call rbind() I get:

Error in match.names(cla         


        
5条回答
  •  情深已故
    2020-11-27 20:38

    You could use rbindlist which takes different column names. Using @LyzandeR's data

    library(data.table) #data.table_1.9.5
    rbindlist(list(a,b))
    #            a         b
    # 1: 0.8403348 0.1579255
    # 2: 0.4759767 0.8182902
    # 3: 0.8091875 0.1080651
    # 4: 0.9846333 0.7035959
    # 5: 0.2153991 0.8744136
    # 6: 0.7604137 0.9753853
    # 7: 0.7553924 0.1210260
    # 8: 0.7315970 0.6196829
    # 9: 0.5619395 0.1120331
    #10: 0.5711995 0.7252631
    

    Update

    Based on the object names of the 12 datasets (i.e. 'Goal1_Costo', 'Goal2_Costo',..., 'Goal12_Costo'),

     nm1 <- paste(paste0('Goal', 1:12), 'Costo', sep="_")
     #or using `sprintf`
     #nm1 <- sprintf('%s%d_%s', 'Goal', 1:12, 'Costo')
     rbindlist(mget(nm1))
    

提交回复
热议问题