Search code examples
rif-statementconditional-statementsuniquemultiple-columns

How to create a new column in R based on a unique conditional statement?


I am trying to create a new column in a large dataset based on a conditional statement where if a unique value appears in one column, that new column will have a 1 and if it is no longer unique it will have a 0. The data I have appears as such:

ex <- data.frame(id = rep(1234, 6), claim = c(14553929, 14553929, 14553929, 2416871, 2416871, 14553947), value = c(27626, 245439, 123435, 764532, 1232467, 3235533))

but I would like it to look like this after the conditional statement:

ex1 <- data.frame(id = rep(1234, 6), claim = c(14553929, 14553929, 14553929, 2416871, 2416871, 14553947),
                 value = c(27626, 245439, 123435, 764532, 1232467, 3235533), flag = c(1,0,0,1,0,1))

so far I've tried doing : ex$flag <- ifelse(unique(ex$claim), 1, 0) but I keep getting an error. I'm not sure if there is an easier way to do this or if I'm missing a basic step, but any help would be greatly appreciated!


Solution

  • We can use duplicated to create the binary column

    ex$flag <-  +(!duplicated(ex$claim))
    ex$flag
    #[1] 1 0 0 1 0 1