54

I have a data frame in R which looks like:

| RIC    | Date                | Open   |
|--------|---------------------|--------|
| S1A.PA | 2011-06-30 20:00:00 | 23.7   |
| ABC.PA | 2011-07-03 20:00:00 | 24.31  |
| EFG.PA | 2011-07-04 20:00:00 | 24.495 |
| S1A.PA | 2011-07-05 20:00:00 | 24.23  |

I want to know if there's any duplicates regarding to the combination of RIC and Date. Is there a function for that in R?

Monica Heddneck
  • 2,973
  • 10
  • 55
  • 89
user802231
  • 853
  • 2
  • 8
  • 10

7 Answers7

81

You can always try simply passing those first two columns to the function duplicated:

duplicated(dat[,1:2])

assuming your data frame is called dat. For more information, we can consult the help files for the duplicated function by typing ?duplicated at the console. This will provide the following sentences:

Determines which elements of a vector or data frame are duplicates of elements with smaller subscripts, and returns a logical vector indicating which elements (rows) are duplicates.

So duplicated returns a logical vector, which we can then use to extract a subset of dat:

ind <- duplicated(dat[,1:2])
dat[ind,]

or you can skip the separate assignment step and simply use:

dat[duplicated(dat[,1:2]),]
joran
  • 169,992
  • 32
  • 429
  • 468
  • How can I retrieve the duplicated rows? I don't know how the results of duplicated function is indexed. – user802231 Aug 08 '11 at 18:34
  • @user802231 - Edited to address your further query. – joran Aug 08 '11 at 18:40
  • 1
    I tried, but the result seems not correct. What I get is showed below: (number in front of each line is row name) RIC Date 107515 7541.T 2011-06-30 20:00:00 107516 7541.T 2011-07-03 20:00:00 107517 7541.T 2011-07-04 20:00:00 107518 7541.T 2011-07-05 20:00:00 107519 7541.T 2011-07-06 20:00:00 107520 7541.T 2011-07-07 20:00:00 107521 7541.T 2011-07-10 20:00:00 107522 7541.T 2011-07-11 20:00:00 107523 7541.T 2011-07-12 20:00:00 107524 7541.T 2011-07-13 20:00:00 107525 7541.T 2011-07-14 20:00:00 107526 7541.T 2011-07-18 20:00:00 – user802231 Aug 10 '11 at 14:14
  • @user802231 What's the problem? – joran Aug 10 '11 at 14:39
  • Watch out with this solution!! It will only return `TRUE` for exactly the same *combination* of columns 1 and 2, not if the digits are inversed. In other words: `A,B` (that is, if these are the values of column 1 and column 2, respectively) will be flagged as duplicate if there's another `A,B`, but *not* if there's a `B,A` – rvrvrv Feb 27 '19 at 18:26
28

dplyr is so much nicer for this sort of thing:

library(dplyr)
yourDataFrame %>%
    distinct(RIC, Date, .keep_all = TRUE)

(the ".keep_all is optional. if not used, it will return only the deduped 2 columns. when used, it returns the deduped whole data frame)

Guy Manova
  • 434
  • 4
  • 7
  • 2
    How would you do it if you just want to know whether duplicate values exist? – erocoar Nov 17 '17 at 13:28
  • 7
    While this is a useful trick in general, it doesn't answer the question the OP posted, which is how one identifies duplicate observations. – shrgm May 17 '18 at 17:35
20

Here's a dplyr option for tagging duplicates based on two (or more) columns. In this case ric and date:

df <- data_frame(ric = c('S1A.PA', 'ABC.PA', 'EFG.PA', 'S1A.PA', 'ABC.PA', 'EFG.PA'),
                 date = c('2011-06-30 20:00:00', '2011-07-03 20:00:00', '2011-07-04 20:00:00', '2011-07-05 20:00:00', '2011-07-03 20:00:00', '2011-07-04 20:00:00'),
                 open = c(23.7, 24.31, 24.495, 24.23, 24.31, 24.495))

df %>% 
  group_by(ric, date) %>% 
  mutate(dupe = n()>1)
# A tibble: 6 x 4
# Groups:   ric, date [4]
  ric    date                 open dupe 
  <chr>  <chr>               <dbl> <lgl>
1 S1A.PA 2011-06-30 20:00:00  23.7 FALSE
2 ABC.PA 2011-07-03 20:00:00  24.3 TRUE 
3 EFG.PA 2011-07-04 20:00:00  24.5 TRUE 
4 S1A.PA 2011-07-05 20:00:00  24.2 FALSE
5 ABC.PA 2011-07-03 20:00:00  24.3 TRUE 
6 EFG.PA 2011-07-04 20:00:00  24.5 TRUE 
sbha
  • 9,802
  • 2
  • 74
  • 62
9

Easy way to get the information you want is to use dplyr.

yourDF %>% 
  group_by(RIC, Date) %>% 
  mutate(num_dups = n(), 
         dup_id = row_number()) %>% 
  ungroup() %>% 
  mutate(is_duplicated = dup_id > 1)

Using this:

  • num_dups tells you how many times that particular combo is duplicated
  • dup_id tells you which duplicate number that particular row is (e.g. 1st, 2nd, or 3rd, etc)
  • is_duplicated gives you an easy condition you can filter on later to remove all the duplicate rows (e.g. filter(!is_duplicated)), though you could also use dup_id for this (e.g. filter(dup_id == 1))
Brandon
  • 1,722
  • 1
  • 19
  • 32
4

If you want to remove duplicate records based on values of Columns Date and State in dataset data.frame:

#Indexes of the duplicate rows that will be removed: 
duplicate_indexes <- which(duplicated(dataset[c('Date', 'State')]),) 
duplicate_indexes 

#new_uniq will contain unique dataset without the duplicates. 
new_uniq <- dataset[!duplicated(dataset[c('Date', 'State')]),] 
View(new_uniq) 
Saurabh Jain
  • 1,600
  • 1
  • 20
  • 30
2

I think what you're looking for is a way to return a data frame of the duplicated rows in the same format as your original data. There is probably a more elegant way to do this but this works:

dup <- data.frame(as.numeric(duplicated(df$var))) #creates df with binary var for duplicated rows
colnames(dup) <- c("dup") #renames column for simplicity
df2 <- cbind(df, dup) #bind to original df
df3 <- subset(df2, dup == 1) #subsets df using binary var for duplicated`
plannapus
  • 18,529
  • 4
  • 72
  • 94
0

Found quite a masterful idea posted by Steve Lianouglou that helps solve this problem with the great advantage of indexing the repetitions:

If you generate a hash column concatenating both your columns for which you want to check duplicates, you can then use dplyr::n() together with seq to give an index to each duplicate occurrence as follows

dat %>% mutate(hash = str_c(RIC, Date)) %>%
  group_by(hash) %>% 
  mutate(duplication_id = seq(n()) %>%
 ungroup ()

Your column duplication_id tells you how many identical rows (same row values for both columns) are there in your table above the one indexed. I used this to remove duplicate Ids.

terraviva
  • 11
  • 4