Search code examples
rgroup-byuniquedplyr

How can I mark (flag) first unique record as 1 and the rest similar records as 0 in data frame in R


I need help with my data in R. I will create new column if I have at least one record. My data (df) look like this:

date adress
28.03 bla
28.03 xyz
17.03 abc
30.03 yxz
24.03 bla 
17.03 abc
23.03 abc
28.03 bla
24.03 bla
24.03 bla

And I want to create new column with driving, where if date is the same and adress too then set 1 (in first spotted record). If someone goes another time (date) in the same place, set 1, but if someone go to the same place and date will be again the same set 0.
Sth like this:

date adress drive
28.03 bla 1
28.03 xyz 1
17.03 abc 1
30.03 yxz 1
24.03 bla 1 
17.03 abc 0
23.03 abc 1
28.03 bla 0
24.03 bla 0
24.03 bla 0

I use dplyr:

df2 <- df %>%
       group_by(date, adress) %>%
       mutate(drive = ifelse(n()>1, 0, 1))

I get data as below and my first spotted record has also 0.

date adress drive
28.03 bla 0
28.03 xyz 1
17.03 abc 0
30.03 yxz 1
24.03 bla 0 
17.03 abc 0
23.03 abc 1
28.03 bla 0
24.03 bla 0
24.03 bla 0

Any has some idea?


Solution

  • use duplicated. If duplicate records it returns 1 else 0, therefore ! bang operator. + before logical converts it to numeric.

    df %>% mutate(drive = +!duplicated(paste(date, adress)))
    
        date adress drive
    1  28.03    bla     1
    2  28.03    xyz     1
    3  17.03    abc     1
    4  30.03    yxz     1
    5  24.03    bla     1
    6  17.03    abc     0
    7  23.03    abc     1
    8  28.03    bla     0
    9  24.03    bla     0
    10 24.03    bla     0