Rbind with new columns and data.table

后端 未结 5 716
借酒劲吻你
借酒劲吻你 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:06

    Here is an approach that will update the missing columns in

    rbind.missing <- function(A, B) { 
    
      cols.A <- names(A)
      cols.B <- names(B)
    
      missing.A <- setdiff(cols.B,cols.A)
      # check and define missing columns in A
      if(length(missing.A) > 0L){
       # .. means "look up one level"
       class.missing.A <- lapply(B[, ..missing.A], class)
       nas.A <- lapply(class.missing.A, as, object = NA)
       A[,c(missing.A) := nas.A]
      }
      # check and define missing columns in B
      missing.B <- setdiff(names(A), cols.B)
      if(length(missing.B) > 0L){
        class.missing.B <- lapply(A[, ..missing.B], class)
        nas.B <- lapply(class.missing.B, as, object = NA)
        B[,c(missing.B) := nas.B]
      }
      # reorder so they are the same
      setcolorder(B, names(A))
      rbind(A, B)
    
    }
    
    rbind.missing(dt.1,dt.2)
    
    ##    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
    

    This will not be efficient for many, or large data.tables, as it only works two at a time.

提交回复
热议问题