[datatable-help] No error message when trying to use a column which doesn't exist

Matthew Dowle mdowle at mdowle.plus.com
Mon Jul 2 22:54:39 CEST 2012


> Hi there,
>
> I have just installed the new version of data.table and used the "fresh"
> function combining *:= in j *with *by*, and I am very happy with it. So
> first I want to thank the maintainers/developers for the library and the
> new function.

Great, nice to hear. Welcome to the list.

> The main reason for my email, though, is the following: I had a variable
> called results62. I created a column of NAs called LAGMKTCAP and
> subsequently tried to change the values of this column using
>
> results62[,LAGMKTCAP :=
> results62$MKTCAP[match(results62$PM,results62$TM)],by=PERMNO]

You don't need the 3 repeats of "results62$". Just :

results62[,LAGMKTCAP:=MKTCAP[match(PM,TM)],by=PERMNO]

That's why j is evaluated within the frame of DT, so you don't have to use
$.  However it's probably better to do that op using a setkey(PERMNO,date)
and then joining with roll=TRUE with lag applied to date.

> without having created the column TM beforehand. I received the output
> from
> R of the head and tail of my object results62, as expected, but the column
> LAGMKTCAP hadn't been changed. Only later did I realise I had forgotten to
> create the column TM.
>
> Wouldn't it be desirable to return an error when trying to use a column of
> the data.table object which doesn't yet exist? Is this normal behaviour or
> a bug?

It's normal and good behaviour for base R's $. Using data.table as above
will return an error if TM doesn't exist at all. If it isn't a column name
but is in calling scope then it will find it there, though.

There is a feature request to add an argument 'inherits=FALSE' to
[.data.table.  That would allow more control. It would mean use column
names and only columns and don't look in calling scope.

HTH.

>
>
>
> Vivianne
> ----------------------------------------------
> “Don't worry about the world coming to an end today. It is already
> tomorrow
> in Australia.”
> _______________________________________________
> 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