get rows of unique values by group

前端 未结 4 737
抹茶落季
抹茶落季 2021-01-19 02:29

I have a data.table and want to pick those lines of the data.table where some values of a variable x are unique relative to another variable y

It\'s possible to get

4条回答
  •  滥情空心
    2021-01-19 03:07

    The simpler data.table solution is to grab the first element of each group

    > dt[, head(.SD, 1), by=.(y, x)]
       y x z
    1: a 1 1
    2: a 2 2
    3: b 3 4
    4: b 2 5
    5: b 1 6
    

提交回复
热议问题