Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

correct braces placement in := within data.table

Tags:

r

data.table

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)]
like image 445
Alex Avatar asked Mar 31 '13 19:03

Alex


People also ask

What is := in data table?

table way. Unlike data. frame, the := operator adds a column to both the object living in the global environment and used in the function.

What is setkey R?

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.

Is data table faster than Dplyr?

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.

What package is data table in R?

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.


1 Answers

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

However, more succinctly and naturally:

you are probably looking for this:

 x[ , c := {b = a + 3; b}]


Update from Matthew

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!

like image 155
Ricardo Saporta Avatar answered Nov 13 '22 07:11

Ricardo Saporta