Skip to content

Commit 59c51ce

Browse files
author
Matt Summersgill
committed
Updated group2NA to avoid constructing .SD and calls to [.data.table
1 parent 46c39fe commit 59c51ce

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

R/group2NA.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,11 @@ group2NA <- function(data, groupNames = "group", nested = NULL, ordered = NULL,
5454

5555
allVars <- c(nested, groupNames, ordered)
5656

57-
# TODO: this is slow
57+
# TODO: better now
5858
d <- if (retrace.first) {
59-
data.table::setDT(data)[, index := .GRP, keyby = allVars][, .SD[c(1:(.N),1,(.N+1))], keyby = index][,index := NULL]
59+
data.table::setDT(data, key = allVars)[ data[, .I[c(seq_along(.I), 1L, .N+1L)], by=allVars]$V1 ]
6060
} else {
61-
data.table::setDT(data)[, index := .GRP, keyby = allVars][, .SD[1:(.N+1)], keyby = index][,index := NULL]
61+
data.table::setDT(data, key = allVars)[ data[, .I[c(seq_along(.I), 1L, .N+1L)], by=allVars]$V1 ]
6262
}
6363

6464
# TODO: how to drop the NAs separating the nested values? Does it even matter?

0 commit comments

Comments
 (0)