Here is an example of a problem I am having. Am I misusing or is this a bug?
require(data.table)
x <- data.table(a = 1:4)
# this does not work
x[ , {b = a + 3; `:=`(c = b)}]
# Error in `:=`(c = b) : unused argument(s) (c = b)
# this works fine
x[ ,`:=`(c = a + 3)]
table way. Unlike data. frame, the := operator adds a column to both the object living in the global environment and used in the function.
Description. setkey sorts a data. table and marks it as sorted with an attribute sorted . The sorted columns are the key. The key can be any number of columns.
I started learning R about a couple of months ago and I've been using dplyr for most of my codes. However, a week ago, I found data. table and immediately my codes that used large datasets became so much faster.
Data. table is an extension of data. frame package in R. It is widely used for fast aggregation of large datasets, low latency add/update/remove of columns, quicker ordered joins, and a fast file reader.
not a bug, it's just that the ordering of the braces should be different:
That is, use the braces to wrap only the RHS
argument in `:=`(LHS, RHS)
Example:
# sample data
x <- data.table(a = 1:4)
# instead of:
x[ , {b = a + 3; `:=`(c, b)}] # <~~ Notice braces are wrapping LHS AND RHS
# use this:
x[ , `:=`(c, {b = a + 3; b})] # <~~ Braces wrapping only RHS
x
# a c
# 1: 1 4
# 2: 2 5
# 3: 3 6
# 4: 4 7
you are probably looking for this:
x[ , c := {b = a + 3; b}]
Exactly. Using :=
in other incorrect ways gives this (long) error :
x := 1
# Error: := is defined for use in j only, and (currently) only once; i.e.,
# DT[i,col:=1L] and DT[,newcol:=sum(colB),by=colA] are ok, but not
# DT[i,col]:=1L, not DT[i]$col:=1L and not DT[,{newcol1:=1L;newcol2:=2L}].
# Please see help(":="). Check is.data.table(DT) is TRUE.
but not in the case that the question showed, giving just :
x[ , {b = a + 3; `:=`(c = b)}]
# Error in `:=`(c = b) : unused argument(s) (c = b)
I've just changed this in v1.8.9. Both these incorrect ways of using :=
now give a more succinct error :
x[ , {b = a + 3; `:=`(c = b)}]
# Error in `:=`(c = b) :
# := and `:=`(...) are defined for use in j only, in particular ways. See
# help(":="). Check is.data.table(DT) is TRUE.
and we'll embellish ?":="
. Thanks @Alex for highlighting!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With