R data.table compute new column, but insert at beginning

后端 未结 1 1294
南旧
南旧 2020-12-19 09:48

In R data.tables, I can use this syntax to add a new column:

> dt <- data.table(a=c(1,2), b=c(3,4))
> dt[, c := a + b]
> dt
   a b c         


        
相关标签:
1条回答
  • 2020-12-19 10:50

    Update: This feature has now been merged into the latest CRAN version of data.table (starting with v1.11.0), so installing the development version is no longer necessary to use this feature. From the release notes:

    1. setcolorder() now accepts less than ncol(DT) columns to be moved to the front, #592. Thanks @MichaelChirico for the PR.

    Current development version of data.table (v1.10.5) has updates to setcolorder() that make this way more convenient by accepting a partial list of columns. The columns provided are placed first, and then all non-specified columns are added after in the existing order.

    Installation instructions for development branch here.

    Note regarding development branch stability: I've been running it for several months now to utilize the multi-threaded version in fread() in v1.10.5 (that alone is worth the update if you deal with multi-GB .csv files) and I have not noticed any bugs or regressions for my usage.

    library(data.table)
    DT <- as.data.table(mtcars)
    DT[1:5]
    

    gives

        mpg cyl disp  hp drat    wt  qsec vs am gear carb
    1: 21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
    2: 21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
    3: 22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
    4: 21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
    5: 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
    

    re-order columns based on a partial list:

    setcolorder(DT,c("gear","carb"))
    DT[1:5]
    

    now gives

       gear carb  mpg cyl disp  hp drat    wt  qsec vs am
    1:    4    4 21.0   6  160 110 3.90 2.620 16.46  0  1
    2:    4    4 21.0   6  160 110 3.90 2.875 17.02  0  1
    3:    4    1 22.8   4  108  93 3.85 2.320 18.61  1  1
    4:    3    1 21.4   6  258 110 3.08 3.215 19.44  1  0
    5:    3    2 18.7   8  360 175 3.15 3.440 17.02  0  0
    

    If for any reason you don't want to update to the development branch, the following works in previous (and current CRAN) versions.

    newCols <- c("gear","carb")
    setcolorder(DT,c(newCols, setdiff(newCols,colnames(DT)) ## (Per Frank's advice in comments)
    
    ## the long way I'd always done before seeing setdiff()
    ## setcolorder(DT,c(newCols,colnames(DT)[which(!colnames(DT) %in% newCols)]))
    
    0 讨论(0)
提交回复
热议问题