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

Matthew Dowle mdowle at mdowle.plus.com
Tue Sep 4 11:14:30 CEST 2012


Agreed, thanks for reporting. Please raise a new
bug.report(package="data.table"). Think you could probably add it in to
#2223, but it's sufficiently different to warrant a new subject line
(helps other users establish if it's known or not).

> 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
> _______________________________________________
> datatable-help mailing list
> datatable-help at lists.r-forge.r-project.org
> https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/datatable-help




More information about the datatable-help mailing list