Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8323853
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T23:51:06+00:00 2026-06-08T23:51:06+00:00

Edit 2019: This question was asked prior to changes in data.table in November 2016,

  • 0

Edit 2019: This question was asked prior to changes in data.table in November 2016, see the accepted answer below for both the current and previous methods.

I have a data.table table with about 2.5 million rows. There are two columns. I want to remove any rows that are duplicated in both columns. Previously for a data.frame I would have done this:
df -> unique(df[,c('V1', 'V2')]) but this doesn’t work with data.table. I have tried unique(df[,c(V1,V2), with=FALSE]) but it seems to still only operate on the key of the data.table and not the whole row.

Any suggestions?

Cheers,
Davy

Example

>dt
      V1   V2
[1,]  A    B
[2,]  A    C
[3,]  A    D
[4,]  A    B
[5,]  B    A
[6,]  C    D
[7,]  C    D
[8,]  E    F
[9,]  G    G
[10,] A    B

in the above data.table where V2 is the table key, only rows 4,7, and 10 would be removed.

dt <- data.table::data.table(
  V1 = c("B", "A", "A", "A", "A", "A", "C", "C", "E", "G"),
  V2 = c("A", "B", "B", "B", "C", "D", "D", "D", "F", "G"),
)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T23:51:08+00:00Added an answer on June 8, 2026 at 11:51 pm

    For v1.9.8+ (released November 2016)

    From ?unique.data.table
    By default all columns are being used (which is consistent with ?unique.data.frame)

    unique(dt)
       V1 V2
    1:  A  B
    2:  A  C
    3:  A  D
    4:  B  A
    5:  C  D
    6:  E  F
    7:  G  G
    

    Or using the by argument in order to get unique combinations of specific columns (like previously keys were used for)

    unique(dt, by = "V2")
       V1 V2
    1:  A  B
    2:  A  C
    3:  A  D
    4:  B  A
    5:  E  F
    6:  G  G
    

    Prior v1.9.8

    From ?unique.data.table, it is clear that calling unique on a data table only works on the key. This means you have to reset the key to all columns before calling unique.

    library(data.table)
    dt <- data.table(
      V1=LETTERS[c(1,1,1,1,2,3,3,5,7,1)],
      V2=LETTERS[c(2,3,4,2,1,4,4,6,7,2)]
    )
    

    Calling unique with one column as key:

    setkey(dt, "V2")
    unique(dt)
         V1 V2
    [1,]  B  A
    [2,]  A  B
    [3,]  A  C
    [4,]  A  D
    [5,]  E  F
    [6,]  G  G
    

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

EDIT 07/14 As Bill Burgess mentionned in a comment of his answer, this question
EDIT: See my answer below--> I am wanting to have a view that when
Edit: The below question was answered by this . I have a new updated
EDIT: looking for this: http://diminishing.org/extending-formtastic-with-a-sprinkle-of-jquery (If this works I'll answer my own question) I've
Edit: I'm looking for solution for this question now also with other programming languages.
EDIT After staring at this for 2 days, I do see one issue. I
EDIT : I've gotten the famous question badge with this question, so I figured
Edit: 2020 update -- Please disregard the question below as Google's weather API is
EDIT: I asked around a bit, and apparently my mistake was this: we usually
EDIT: This question was exceptionally dumb and made me look like a script kiddie,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.