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

Michael Nelson michael.nelson at sydney.edu.au
Mon Aug 20 02:06:01 CEST 2012


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


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/datatable-help/attachments/20120820/16f60deb/attachment.html>


More information about the datatable-help mailing list