The reason why you are getting this error message is that when using with=FALSE
you tell data.table to treat j
as if it were a dataframe. It therefore expects a vector of columnnames and not an expression to be evaluated in j
as new=.N
.
From the documentation of ?data.table
about with
:
By default with=TRUE
and j is evaluated within the frame of x; column
names can be used as variables. When with=FALSE
j is a character
vector of column names or a numeric vector of column positions to
select, and the value returned is always a data.table.
When you use with=FALSE
, you have to select the columnnames in j
without a .
before ()
like this: dt1[, (colShow), with=FALSE]
. Other options are dt1[, c(colShow), with=FALSE]
or dt1[, colShow, with=FALSE]
. The same result can be obtained by using dt1[, .(col3)]
To sum up: with = FALSE
is used to select columns the data.frame way. So, you should do it then as such.
Also by using by = colBy
you are telling data.table to evaluate j
which is in contradiction with with = FALSE
.
From the documentation of ?data.table
about j
:
A single column name, single expresson of column names, list()
of
expressions of column names, an expression or function call that
evaluates to list (including data.frame and data.table which are
lists, too), or (when with=FALSE
) a vector of names or positions to
select.
j
is evaluated within the frame of the data.table; i.e., it
sees column names as if they are variables. Use j=list(...)
to return
multiple columns and/or expressions of columns. A single column or
single expression returns that type, usually a vector. See the
examples.
See also points 1.d and 1.g of the introduction vignette of data.table.
ansvals
is a name used in data.table internals. You can see where it appears in the code by using ctrl+f (Windows) or cmd+f (macOS) here.