Rbind with new columns and data.table

后端 未结 5 702
借酒劲吻你
借酒劲吻你 2020-12-30 07:38

I need to add many large tables to an existing table, so I use rbind with the excellent package data.table. But some of the later tables have more columns than the original

5条回答
  •  不思量自难忘°
    2020-12-30 08:08

    Since v1.9.2, data.table's rbind function gained fill argument. From ?rbind.data.table documentation:

    If TRUE fills missing columns with NAs. By default FALSE. When TRUE, use.names has to be TRUE, and all items of the input list has to have non-null column names.

    Thus you can do (prior to approx v1.9.6):

    data.table::rbind(dt.1, dt.2, fill=TRUE) 
    #    aa bb cc
    # 1:  1  2 NA
    # 2:  2  3 NA
    # 3:  3  4 NA
    # 4:  1  2  3
    # 5:  2  3  4
    # 6:  3  4  5
    

    UPDATE for v1.9.6:

    This now works directly:

    rbind(dt.1, dt.2, fill=TRUE)
    #    aa bb cc
    # 1:  1  2 NA
    # 2:  2  3 NA
    # 3:  3  4 NA
    # 4:  1  2  3
    # 5:  2  3  4
    # 6:  3  4  5
    

提交回复
热议问题