Search code examples
rduplicatesconcatenationstring-concatenation

How to use duplicated()


I have a dataset which has customer purchase information. I have tried to create unique id by concatenating device_id(of the customer), store_id, product_id and date (of purchase). I have used the following code for this

customer$device_store_product_date <- paste(customer$device, customer$store_id, customer$product_id, customer$date, sep='_')

The resultant column is something like this:

        device_store_product_date
48c6eec37affa1db_203723_9313962_2016-02-19
eb2c2f00071b97f3_179926_6180944_2016-02-20
d82066a784c9552_180704_9308311_2016-02-20
9766bba65b1ef9ac_204187_9313852_2016-02-20
77d80c1066f5267_180488_9312672_2016-02-20

As expected there are still duplicates. To identify them i used duplicated():

x1 = customer[duplicated(customer$device_store_product_date),]

However, for few of the x1$device_store_product_date only single entries are present. This should not be the case as x1 should consist of repeated values. Let me know where am i going wrong. To select entries corresponding to a particular value of device_store_product_date i have used:

filter(x1, x1$device_store_product_date=="14163e6b6ed06890_203723_9313477_2016-02-20")

Solution

  • duplicated() returns TRUE for any value that has already occurred, so

    x <-c("a","b","a")
    duplicated(x)
    

    will return

    FALSE FALSE TRUE
    

    If you want to get all the first occurrence as well, something like this will work

    duplicated(x)|rev(duplicated(rev(x)))