Unique doesn't use keys as default anymore

后端 未结 1 909
广开言路
广开言路 2021-01-28 01:28

I mainly use Rstudio in Mac. Recently I had to start using Windows. However, I found out that unique() does not provide unique rows in data.table based on the key. Here is an ex

相关标签:
1条回答
  • 2021-01-28 01:57

    That's because you have different data.table versions on both. On Mac you have a <1.9.8 version (which still uses keys as default), while on Windows you have a newer version (which doesn't).

    As stated in ?unique (in data.table V1.9.8+):

    By default all columns are being used. This was changed recently for consistency to data.frame methods. In version < 1.9.8 default was key(x)

    Meaning, from now on, you need to explicitly specify the by variable even if you already have keys set, otherwise it will just use all the columns.

    For your specific example, this works

    unique(e, by = "a")
    #    a b
    # 1: 2 a
    # 2: 3 a
    # 3: 5 d
    # 4: 6 t
    # 5: 7 l
    

    Or as @Frank mentioned in comments, you can also specify the the key in the by param using unique(a, by = key(a)).

    0 讨论(0)
提交回复
热议问题