Quantcast

using lapply(.SD...) with base operators

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

using lapply(.SD...) with base operators

Michael Nelson
I have just noticed (while answering this SO question  

a quirk in referencing operators within a call to lapply(.SD,...)

In base r, 

lapply(1:4,'+',1) 

will work 

however

DT <- data.table(id=rep(1:5,20), a=1:100, b=sample(1:100, 100), c=sample(1:100, 100))

DT[,lapply(.SD, '+', 1), by = id]

returns 'attempt to apply non-function'

Ensuring that the base operator is called works

DT[,lapply(.SD, base::'+', 1), by = id]

as does 

DT[,lapply(.SD, Primitive('+'),1), by = id]

which is quicker

Would it be worth noting this in one of the vignettes?

Regards

Michael



_______________________________________________
datatable-help mailing list
[hidden email]
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/datatable-help
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: using lapply(.SD...) with base operators

Matthew Dowle

Interesting. Thanks for posting about this.

The reason is optimization in v1.8.2. Turning on verbosity reveals what
has happened :

> DT[,lapply(.SD, '+', 1), by = id,verbose=TRUE]
..snip..
Optimized j from 'lapply(.SD, "+", 1)' to 'list("+"(a, 1), "+"(b, 1),
"+"(c, 1))'
..snip..
  attempt to apply non-function

Using `` instead of '' works :

    DT[,lapply(.SD, `+`, 1), by = id]

or turning off optimization also works :

    options(datatable.optimize=0)
    DT[,lapply(.SD, '+', 1), by = id]

Should be able to fix this. Have raised bug report #2212 :
https://r-forge.r-project.org/tracker/index.php?func=detail&aid=2212&group_id=240&atid=975

Matthew


> I have just noticed (while answering this SO question
> http://stackoverflow.com/questions/12024098/return-type-for-j-parameter-in-data-table/12030867#12030867
>
> a quirk in referencing operators within a call to lapply(.SD,...)
>
> In base r,
>
> lapply(1:4,'+',1)
>
> will work
>
> however
>
> DT <- data.table(id=rep(1:5,20), a=1:100, b=sample(1:100, 100),
> c=sample(1:100, 100))
>
> DT[,lapply(.SD, '+', 1), by = id]
>
> returns 'attempt to apply non-function'
>
> Ensuring that the base operator is called works
>
> DT[,lapply(.SD, base::'+', 1), by = id]
>
> as does
>
> DT[,lapply(.SD, Primitive('+'),1), by = id]
>
> which is quicker
>
> Would it be worth noting this in one of the vignettes?
>
> Regards
>
> Michael
>
>
> _______________________________________________
> datatable-help mailing list
> [hidden email]
> https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/datatable-help


_______________________________________________
datatable-help mailing list
[hidden email]
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/datatable-help
Loading...