Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
497 views
in Technique[技术] by (71.8m points)

r - unique.data.table select last row in place of the first

calling unique on a keyed data.table you'll have unique lines per each group. In case of duplicated lines the first will be taken. When I need the take the last instead ( in general the last temporal transaction) I use .SD[.N]

library(data.table)
library(microbenchmark)

dt <- data.table(id=sample(letters, 10000, T), var=rnorm(10000), key="id")

microbenchmark(unique(dt), dt[, .SD[.N], by=id])
Unit: microseconds
                   expr      min        lq    median       uq        max neval
             unique(dt)  570.882  586.1155  595.8975  608.406   3209.122   100
 dt[, .SD[.N], by = id] 6532.739 6637.7745 6694.3820 6776.968 208264.433   100

do you know a faster way to do the same?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Create a data.table that contains the unique combinations of the key variables then join using mult = 'last'

Using .SD is convenient, but slow. You could use .I instead if you wished.

dtu <- unique(dt)[,key(dt), with = FALSE]
dt[dtu, mult = 'last']

Or

 dt[ dt[,  .I[.N], by = key(dt)]$V1]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...