Keep first row by multiple columns in an R data.table

好久不见. 提交于 2019-11-27 15:18:01

data.table provides S3 methods for unique, duplicated and anyDuplicated

unique(dt, by = c('x','y'))

will give you what you want.

data.table does duplicated by key. From ?duplicated.data.table:

 ‘duplicated’ returns a logical vector indicating which rows of a
 ‘data.table’ have duplicate rows (by key).


setkey(dt, x, y)
dt[!duplicated(dt)]
##    x y z
## 1: 1 1 1
## 2: 1 2 1
## 3: 2 2 2
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!