[datatable-help] using lapply(.SD...) with base operators

Matthew Dowle mdowle at mdowle.plus.com
Mon Aug 20 13:47:04 CEST 2012


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
> 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