[datatable-help] Output when using mean() with na.rm

Matthew Dowle mdowle at mdowle.plus.com
Thu Jun 9 19:28:16 CEST 2011


Interesting.   See FAQ 3.1, first bullet, for background on why it's doing 
all.vars(jsub).

TRUE is a reserved word in R, so isn't considered a variable. T is a 
variable (an object) in base holding the value TRUE (and may be masked by 
user which is why T isn't recommended).

That leads to the more general bug :

> myvar = 6
> table2[A=="b",B*myvar]
Error in `[.data.table`(x, irows, all.vars(jsub), with = FALSE) :
  undefined columns selected

It's just (I think) when i is integer or logical, and j uses a variable in 
calling scope.  When i is data.table, should be fine.

A workaround in the meantime is :

table2[A=="b",mean(B,na.rm=get("T"))]

Bug #1421 now raised, thanks :

https://r-forge.r-project.org/tracker/index.php?func=detail&aid=1421&group_id=240&atid=975

Matthew



"Alexander Peterhansl" <APeterhansl at GAINCapital.com> wrote in message 
news:FD59D3740610AB4388AA1BDC7FC9531D7971F8 at CRP11EXHMBS01.GAINCapital.com...

Dear Data Table Help List,

I am using data.table version 1.6 (with R version 2.13.0, 64-bit on Windows 
7) and I've come across some strange behavior.

Please look at the following example, where I simply take the mean of two 
numbers.

> table1 <- data.table(A=c("a","b","b"),B=c(4,5,NA))
> setkey(table1,A)
> table1["b",mean(B,na.rm=T)]
     A V1
[1,] b  5
# EXPECTED OUTPUT

> table2 <- data.table(A=c("a","b","b"),B=c(4,5,NA))
> table2[A=="b",mean(B,na.rm=TRUE)]
[1] 5
# LESS EFFICIENT, BUT EXPECTED OUTPUT

> table2[A=="b",mean(B,na.rm=T)]
Error in `[.data.table`(x, irows, all.vars(jsub), with = FALSE) :
  undefined columns selected
# WHY THE ERROR?

Any help would be much appreciated.

Regards,
Alex



_______________________________________________
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