Skip to content

gfirst(.SD) throws an error about not using head(.SD, n), but the latter works #2030

@franknarf1

Description

@franknarf1

I tried this and got an error:

library(data.table)
DT = data.table(id = c(1L,1L,2L), v = 1:3)
DT[, first(.SD), by=id, .SDcols="v", verbose = TRUE]
# Finding groups using forderv ... 0 sec
# Finding group sizes from the positions (can be avoided to save RAM) ... 0 sec
# lapply optimization is on, j unchanged as 'first(.SD)'
# GForce optimized j to 'gfirst(.SD)'

Error in gfirst(.SD) :
GForce head can only be applied to columns, not .SD or similar. To get head of all items in a list such as .SD, either add the prefix utils::head(.SD) or turn off GForce optimization using options(datatable.optimize=1).

I tried head and it did work, contrary to the error message:

DT[, head(.SD, 1L), by=id, .SDcols="v", verbose=TRUE]
# Finding groups using forderv ... 0.02 sec
# Finding group sizes from the positions (can be avoided to save RAM) ... 0 sec
# lapply optimization changed j from 'head(.SD, 1L)' to 'list(head(v, 1L))'
# GForce optimized j to 'list(ghead(v, 1L))'
# Making each group and running j (GForce TRUE) ... 0 secs
#    id v
# 1:  1 1
# 2:  2 3

Not a big deal and maybe it'll be handled in the process of of doing #735

Metadata

Metadata

Assignees

Labels

GForceissues relating to optimized grouping calculations (GForce)bug

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions