[datatable-help] Inconsistent results using := and with = F

Michael Nelson michael.nelson at sydney.edu.au
Tue Sep 4 07:16:32 CEST 2012


This is probabily related to Bug


    [#2223] Deleting multiple columns out-of-order using := causes seg fault


However, using `:=` with  `with = F` does not appear to work as intended.

    DT <- data.table(c1 = 1:2)

    DT[, `:=`(c("c2", "c1"), list(c1 * 10, NULL)), with = FALSE]

    ## Warning: Adding new column 'c2' then assigning NULL (deleting it).

    ##    c1
    ## 1: 10
    ## 2: 20


The new column should be called `c2` and it be the only column in DT.



The following gives an appropriate warning (and result)

    DT <- data.table(c1 = 1:2)

    DT[, `:=`(c("c1", "c2"), list(c1 * 10, NULL)), with = FALSE]

    ## Warning: Adding new column 'c2' then assigning NULL (deleting it).

    ##    c1
    ## 1: 10
    ## 2: 20


Obviously this is a non-standard (and non-reccomended) approach. The following would be considered more *standard*


    DT <- data.table(c1 = 1:2)
    # save as a new data.table
    DT2 <- DT[, list(c2 = 10 * c1)]

    # use setnames
    DT <- data.table(c1 = 1:2)
    setnames(DT[, `:=`(c1, 10 * c1)], "c1", "c2")

Regards,

Michael
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/datatable-help/attachments/20120904/20c1e31f/attachment.html>


More information about the datatable-help mailing list